blob: b1a9cd5a3233fc03a722e9699a14ac88fe84f9c1 [file] [log] [blame]
Jason Wanga3f04be2008-11-28 21:36:51 +00001/*
2 * This file is part of the flashrom project.
3 *
Jason Wang13f98ce2008-11-29 15:07:15 +00004 * Copyright (C) 2008 Wang Qingpei <Qingpei.Wang@amd.com>
5 * Copyright (C) 2008 Joe Bao <Zheng.Bao@amd.com>
Uwe Hermann97e8f222009-04-13 21:35:49 +00006 * Copyright (C) 2008 Advanced Micro Devices, Inc.
Wei Hu31402ee2014-05-16 21:39:33 +00007 * Copyright (C) 2009, 2010, 2013 Carl-Daniel Hailfinger
8 * Copyright (C) 2013 Stefan Tauner
Jason Wanga3f04be2008-11-28 21:36:51 +00009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000025#if defined(__i386__) || defined(__x86_64__)
26
Rudolf Marek70e14592013-07-25 22:58:56 +000027#include <string.h>
28#include <stdlib.h>
Jason Wanga3f04be2008-11-28 21:36:51 +000029#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000030#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000031#include "hwaccess.h"
Jason Wanga3f04be2008-11-28 21:36:51 +000032#include "spi.h"
33
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000034/* This struct is unused, but helps visualize the SB600 SPI BAR layout.
35 *struct sb600_spi_controller {
36 * unsigned int spi_cntrl0; / * 00h * /
37 * unsigned int restrictedcmd1; / * 04h * /
38 * unsigned int restrictedcmd2; / * 08h * /
39 * unsigned int spi_cntrl1; / * 0ch * /
40 * unsigned int spi_cmdvalue0; / * 10h * /
41 * unsigned int spi_cmdvalue1; / * 14h * /
42 * unsigned int spi_cmdvalue2; / * 18h * /
43 * unsigned int spi_fakeid; / * 1Ch * /
44 *};
45 */
Jason Wanga3f04be2008-11-28 21:36:51 +000046
Michael Karcherb05b9e12010-07-22 18:04:19 +000047static uint8_t *sb600_spibar = NULL;
Stefan Tauner463dd692013-08-08 12:00:19 +000048enum amd_chipset {
49 CHIPSET_AMD_UNKNOWN,
50 CHIPSET_SB6XX,
51 CHIPSET_SB7XX, /* SP5100 too */
52 CHIPSET_SB89XX, /* Hudson-1 too */
53 CHIPSET_HUDSON234,
Martin Roth82b6ec12014-07-15 13:50:58 +000054 CHIPSET_BOLTON,
Stefan Tauner463dd692013-08-08 12:00:19 +000055 CHIPSET_YANGTZE,
56};
57static enum amd_chipset amd_gen = CHIPSET_AMD_UNKNOWN;
58
Stefan Taunerd5b2aef2014-05-16 21:39:28 +000059#define FIFO_SIZE_OLD 8
Wei Hu31402ee2014-05-16 21:39:33 +000060#define FIFO_SIZE_YANGTZE 71
Stefan Taunerd5b2aef2014-05-16 21:39:28 +000061
62static int sb600_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
63 const unsigned char *writearr, unsigned char *readarr);
Wei Hu31402ee2014-05-16 21:39:33 +000064static int spi100_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
65 const unsigned char *writearr, unsigned char *readarr);
Stefan Taunerd5b2aef2014-05-16 21:39:28 +000066
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000067static struct spi_master spi_master_sb600 = {
Stefan Taunerd5b2aef2014-05-16 21:39:28 +000068 .type = SPI_CONTROLLER_SB600,
69 .max_data_read = FIFO_SIZE_OLD,
70 .max_data_write = FIFO_SIZE_OLD - 3,
71 .command = sb600_spi_send_command,
72 .multicommand = default_spi_send_multicommand,
73 .read = default_spi_read,
74 .write_256 = default_spi_write_256,
75 .write_aai = default_spi_write_aai,
76};
77
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000078static struct spi_master spi_master_yangtze = {
Wei Hu31402ee2014-05-16 21:39:33 +000079 .type = SPI_CONTROLLER_YANGTZE,
80 .max_data_read = FIFO_SIZE_YANGTZE - 3, /* Apparently the big SPI 100 buffer is not a ring buffer. */
81 .max_data_write = FIFO_SIZE_YANGTZE - 3,
82 .command = spi100_spi_send_command,
83 .multicommand = default_spi_send_multicommand,
84 .read = default_spi_read,
85 .write_256 = default_spi_write_256,
86 .write_aai = default_spi_write_aai,
87};
88
Stefan Tauner463dd692013-08-08 12:00:19 +000089static void determine_generation(struct pci_dev *dev)
90{
91 amd_gen = CHIPSET_AMD_UNKNOWN;
Stefan Tauner4442b812013-09-12 15:48:35 +000092 msg_pdbg2("Trying to determine the generation of the SPI interface... ");
93 if (dev->device_id == 0x438d) {
94 amd_gen = CHIPSET_SB6XX;
95 msg_pdbg("SB6xx detected.\n");
96 } else if (dev->device_id == 0x439d) {
97 struct pci_dev *smbus_dev = pci_dev_find(0x1002, 0x4385);
98 if (smbus_dev == NULL)
99 return;
100 uint8_t rev = pci_read_byte(smbus_dev, PCI_REVISION_ID);
101 if (rev >= 0x39 && rev <= 0x3D) {
102 amd_gen = CHIPSET_SB7XX;
103 msg_pdbg("SB7xx/SP5100 detected.\n");
104 } else if (rev >= 0x40 && rev <= 0x42) {
105 amd_gen = CHIPSET_SB89XX;
106 msg_pdbg("SB8xx/SB9xx/Hudson-1 detected.\n");
107 } else {
108 msg_pwarn("SB device found but SMBus revision 0x%02x does not match known values.\n"
109 "Assuming SB8xx/SB9xx/Hudson-1. Please send a log to flashrom@flashrom.org\n",
110 rev);
111 amd_gen = CHIPSET_SB89XX;
112 }
113 } else if (dev->device_id == 0x780e) {
Stefan Tauner463dd692013-08-08 12:00:19 +0000114 /* The PCI ID of the LPC bridge doesn't change between Hudson-2/3/4 and Yangtze (Kabini/Temash)
115 * although they use different SPI interfaces. */
116#ifdef USE_YANGTZE_HEURISTICS
117 /* This heuristic accesses the SPI interface MMIO BAR at locations beyond those supported by
118 * Hudson in the hope of getting 0xff readback on older chipsets and non-0xff readback on
119 * Yangtze (and newer, compatible chipsets). */
120 int i;
121 msg_pdbg("Checking for AMD Yangtze (Kabini/Temash) or later... ");
122 for (i = 0x20; i <= 0x4f; i++) {
123 if (mmio_readb(sb600_spibar + i) != 0xff) {
124 amd_gen = CHIPSET_YANGTZE;
125 msg_pdbg("found.\n");
126 return;
127 }
128 }
129 msg_pdbg("not found. Assuming Hudson.\n");
130 amd_gen = CHIPSET_HUDSON234;
131#else
132 struct pci_dev *smbus_dev = pci_dev_find(0x1022, 0x780B);
133 if (smbus_dev == NULL) {
134 msg_pdbg("No SMBus device with ID 1022:780B found.\n");
135 return;
136 }
137 uint8_t rev = pci_read_byte(smbus_dev, PCI_REVISION_ID);
138 if (rev >= 0x11 && rev <= 0x15) {
139 amd_gen = CHIPSET_HUDSON234;
140 msg_pdbg("Hudson-2/3/4 detected.\n");
Martin Roth82b6ec12014-07-15 13:50:58 +0000141 } else if (rev == 0x16) {
142 amd_gen = CHIPSET_BOLTON;
143 msg_pdbg("Bolton detected.\n");
Stefan Tauner5c316f92015-02-08 21:57:52 +0000144 } else if ((rev >= 0x39 && rev <= 0x3A) || rev == 0x42) {
Stefan Tauner463dd692013-08-08 12:00:19 +0000145 amd_gen = CHIPSET_YANGTZE;
146 msg_pdbg("Yangtze detected.\n");
147 } else {
148 msg_pwarn("FCH device found but SMBus revision 0x%02x does not match known values.\n"
149 "Please report this to flashrom@flashrom.org and include this log and\n"
150 "the output of lspci -nnvx, thanks!.\n", rev);
151 }
152#endif
Stefan Tauner4442b812013-09-12 15:48:35 +0000153 } else
154 msg_pwarn("%s: Unknown LPC device %" PRIx16 ":%" PRIx16 ".\n"
155 "Please report this to flashrom@flashrom.org and include this log and\n"
156 "the output of lspci -nnvx, thanks!\n",
157 __func__, dev->vendor_id, dev->device_id);
Stefan Tauner463dd692013-08-08 12:00:19 +0000158}
Jason Wanga3f04be2008-11-28 21:36:51 +0000159
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +0000160static void reset_internal_fifo_pointer(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000161{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000162 mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +0000163
Rudolf Marek70e14592013-07-25 22:58:56 +0000164 /* FIXME: This loop needs a timeout and a clearer message. */
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000165 while (mmio_readb(sb600_spibar + 0xD) & 0x7)
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000166 msg_pspew("reset\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000167}
168
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000169static int compare_internal_fifo_pointer(uint8_t want)
170{
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000171 uint8_t have = mmio_readb(sb600_spibar + 0xd) & 0x07;
172 want %= FIFO_SIZE_OLD;
173 if (have != want) {
174 msg_perr("AMD SPI FIFO pointer corruption! Pointer is %d, wanted %d\n", have, want);
175 msg_perr("Something else is accessing the flash chip and causes random corruption.\n"
176 "Please stop all applications and drivers and IPMI which access the flash chip.\n");
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000177 return 1;
178 } else {
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000179 msg_pspew("AMD SPI FIFO pointer is %d, wanted %d\n", have, want);
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000180 return 0;
181 }
182}
183
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000184/* Check the number of bytes to be transmitted and extract opcode. */
185static int check_readwritecnt(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt)
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000186{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000187 unsigned int maxwritecnt = flash->mst->spi.max_data_write + 3;
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000188 if (writecnt > maxwritecnt) {
189 msg_pinfo("%s: SPI controller can not send %d bytes, it is limited to %d bytes\n",
190 __func__, writecnt, maxwritecnt);
191 return SPI_INVALID_LENGTH;
192 }
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000193
Stefan Tauner6697f712014-08-06 15:09:15 +0000194 unsigned int maxreadcnt = flash->mst->spi.max_data_read;
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000195 if (readcnt > maxreadcnt) {
196 msg_pinfo("%s: SPI controller can not receive %d bytes, it is limited to %d bytes\n",
197 __func__, readcnt, maxreadcnt);
198 return SPI_INVALID_LENGTH;
199 }
200 return 0;
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000201}
202
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +0000203static void execute_command(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000204{
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000205 msg_pspew("Executing... ");
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000206 mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2);
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000207 while (mmio_readb(sb600_spibar + 2) & 1)
Jason Wanga3f04be2008-11-28 21:36:51 +0000208 ;
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000209 msg_pspew("done\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000210}
211
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000212static int sb600_spi_send_command(struct flashctx *flash, unsigned int writecnt,
213 unsigned int readcnt,
214 const unsigned char *writearr,
215 unsigned char *readarr)
Jason Wanga3f04be2008-11-28 21:36:51 +0000216{
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000217 /* First byte is cmd which can not be sent through the FIFO. */
Jason Wanga3f04be2008-11-28 21:36:51 +0000218 unsigned char cmd = *writearr++;
Jason Wanga3f04be2008-11-28 21:36:51 +0000219 writecnt--;
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000220 msg_pspew("%s, cmd=0x%02x, writecnt=%d, readcnt=%d\n", __func__, cmd, writecnt, readcnt);
221 mmio_writeb(cmd, sb600_spibar + 0);
Jason Wanga3f04be2008-11-28 21:36:51 +0000222
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000223 int ret = check_readwritecnt(flash, writecnt, readcnt);
224 if (ret != 0)
225 return ret;
Jason Wanga3f04be2008-11-28 21:36:51 +0000226
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000227 /* This is a workaround for a bug in SPI controller. If we only send
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000228 * an opcode and no additional data/address, the SPI controller will
229 * read one byte too few from the chip. Basically, the last byte of
230 * the chip response is discarded and will not end up in the FIFO.
231 * It is unclear if the CS# line is set high too early as well.
232 */
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000233 unsigned int readoffby1 = (writecnt > 0) ? 0 : 1;
234 uint8_t readwrite = (readcnt + readoffby1) << 4 | (writecnt);
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000235 mmio_writeb(readwrite, sb600_spibar + 1);
Jason Wanga3f04be2008-11-28 21:36:51 +0000236
Jason Wanga3f04be2008-11-28 21:36:51 +0000237 reset_internal_fifo_pointer();
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000238 msg_pspew("Filling FIFO: ");
239 int count;
240 for (count = 0; count < writecnt; count++) {
241 msg_pspew("[%02x]", writearr[count]);
242 mmio_writeb(writearr[count], sb600_spibar + 0xC);
Jason Wanga3f04be2008-11-28 21:36:51 +0000243 }
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000244 msg_pspew("\n");
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000245 if (compare_internal_fifo_pointer(writecnt))
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000246 return SPI_PROGRAMMER_ERROR;
Jason Wanga3f04be2008-11-28 21:36:51 +0000247
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000248 /*
249 * We should send the data in sequence, which means we need to reset
250 * the FIFO pointer to the first byte we want to send.
251 */
252 reset_internal_fifo_pointer();
Jason Wanga3f04be2008-11-28 21:36:51 +0000253 execute_command();
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000254 if (compare_internal_fifo_pointer(writecnt + readcnt))
255 return SPI_PROGRAMMER_ERROR;
Jason Wanga3f04be2008-11-28 21:36:51 +0000256
257 /*
258 * After the command executed, we should find out the index of the
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000259 * received byte. Here we just reset the FIFO pointer and skip the
260 * writecnt.
261 * It would be possible to increase the FIFO pointer by one instead
262 * of reading and discarding one byte from the FIFO.
263 * The FIFO is implemented on top of an 8 byte ring buffer and the
264 * buffer is never cleared. For every byte that is shifted out after
265 * the opcode, the FIFO already stores the response from the chip.
266 * Usually, the chip will respond with 0x00 or 0xff.
Jason Wanga3f04be2008-11-28 21:36:51 +0000267 */
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000268 reset_internal_fifo_pointer();
Jason Wanga3f04be2008-11-28 21:36:51 +0000269
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000270 /* Skip the bytes we sent. */
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000271 msg_pspew("Skipping: ");
Jason Wanga3f04be2008-11-28 21:36:51 +0000272 for (count = 0; count < writecnt; count++) {
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000273 msg_pspew("[%02x]", mmio_readb(sb600_spibar + 0xC));
Jason Wanga3f04be2008-11-28 21:36:51 +0000274 }
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000275 msg_pspew("\n");
276 if (compare_internal_fifo_pointer(writecnt))
277 return SPI_PROGRAMMER_ERROR;
Jason Wanga3f04be2008-11-28 21:36:51 +0000278
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000279 msg_pspew("Reading FIFO: ");
280 for (count = 0; count < readcnt; count++) {
281 readarr[count] = mmio_readb(sb600_spibar + 0xC);
282 msg_pspew("[%02x]", readarr[count]);
Jason Wanga3f04be2008-11-28 21:36:51 +0000283 }
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000284 msg_pspew("\n");
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000285 if (compare_internal_fifo_pointer(writecnt+readcnt))
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000286 return SPI_PROGRAMMER_ERROR;
287
288 if (mmio_readb(sb600_spibar + 1) != readwrite) {
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000289 msg_perr("Unexpected change in AMD SPI read/write count!\n");
290 msg_perr("Something else is accessing the flash chip and causes random corruption.\n"
291 "Please stop all applications and drivers and IPMI which access the flash chip.\n");
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000292 return SPI_PROGRAMMER_ERROR;
293 }
Jason Wanga3f04be2008-11-28 21:36:51 +0000294
295 return 0;
296}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000297
Wei Hu31402ee2014-05-16 21:39:33 +0000298static int spi100_spi_send_command(struct flashctx *flash, unsigned int writecnt,
299 unsigned int readcnt,
300 const unsigned char *writearr,
301 unsigned char *readarr)
302{
303 /* First byte is cmd which can not be sent through the buffer. */
304 unsigned char cmd = *writearr++;
305 writecnt--;
306 msg_pspew("%s, cmd=0x%02x, writecnt=%d, readcnt=%d\n", __func__, cmd, writecnt, readcnt);
307 mmio_writeb(cmd, sb600_spibar + 0);
308
309 int ret = check_readwritecnt(flash, writecnt, readcnt);
310 if (ret != 0)
311 return ret;
312
313 /* Use the extended TxByteCount and RxByteCount registers. */
314 mmio_writeb(writecnt, sb600_spibar + 0x48);
315 mmio_writeb(readcnt, sb600_spibar + 0x4b);
316
317 msg_pspew("Filling buffer: ");
318 int count;
319 for (count = 0; count < writecnt; count++) {
320 msg_pspew("[%02x]", writearr[count]);
321 mmio_writeb(writearr[count], sb600_spibar + 0x80 + count);
322 }
323 msg_pspew("\n");
324
325 execute_command();
326
327 msg_pspew("Reading buffer: ");
328 for (count = 0; count < readcnt; count++) {
329 readarr[count] = mmio_readb(sb600_spibar + 0x80 + (writecnt + count) % FIFO_SIZE_YANGTZE);
330 msg_pspew("[%02x]", readarr[count]);
331 }
332 msg_pspew("\n");
333
334 return 0;
335}
336
Stefan Taunera6a0d202013-09-15 14:17:39 +0000337struct spispeed {
338 const char *const name;
339 const uint8_t speed;
340};
341
342static const struct spispeed spispeeds[] = {
343 { "66 MHz", 0x00 },
344 { "33 MHz", 0x01 },
345 { "22 MHz", 0x02 },
346 { "16.5 MHz", 0x03 },
Wei Hu31402ee2014-05-16 21:39:33 +0000347 { "100 MHz", 0x04 },
348 { "Reserved", 0x05 },
349 { "Reserved", 0x06 },
350 { "800 kHz", 0x07 },
Stefan Taunera6a0d202013-09-15 14:17:39 +0000351};
352
353static int set_speed(struct pci_dev *dev, const struct spispeed *spispeed)
354{
355 bool success = false;
356 uint8_t speed = spispeed->speed;
357
358 msg_pdbg("Setting SPI clock to %s (0x%x).\n", spispeed->name, speed);
Wei Hu31402ee2014-05-16 21:39:33 +0000359 if (amd_gen >= CHIPSET_YANGTZE) {
360 rmmio_writew((speed << 12) | (speed << 8) | (speed << 4) | speed, sb600_spibar + 0x22);
361 uint16_t tmp = mmio_readw(sb600_spibar + 0x22);
362 success = (((tmp >> 12) & 0xf) == speed && ((tmp >> 8) & 0xf) == speed &&
363 ((tmp >> 4) & 0xf) == speed && ((tmp >> 0) & 0xf) == speed);
364 } else {
Stefan Taunera6a0d202013-09-15 14:17:39 +0000365 rmmio_writeb((mmio_readb(sb600_spibar + 0xd) & ~(0x3 << 4)) | (speed << 4), sb600_spibar + 0xd);
366 success = (speed == ((mmio_readb(sb600_spibar + 0xd) >> 4) & 0x3));
367 }
368
369 if (!success) {
370 msg_perr("Setting SPI clock failed.\n");
371 return 1;
372 }
373 return 0;
374}
375
Wei Hu31402ee2014-05-16 21:39:33 +0000376static int set_mode(struct pci_dev *dev, uint8_t read_mode)
377{
378 uint32_t tmp = mmio_readl(sb600_spibar + 0x00);
379 tmp &= ~(0x6 << 28 | 0x1 << 18); /* Clear mode bits */
380 tmp |= ((read_mode & 0x6) << 28) | ((read_mode & 0x1) << 18);
381 rmmio_writel(tmp, sb600_spibar + 0x00);
382 if (tmp != mmio_readl(sb600_spibar + 0x00))
383 return 1;
384 return 0;
385}
386
Stefan Taunera6a0d202013-09-15 14:17:39 +0000387static int handle_speed(struct pci_dev *dev)
388{
389 uint32_t tmp;
Carl-Daniel Hailfinger57cdd6b2016-03-12 19:49:14 +0000390 uint8_t spispeed_idx = 3; /* Default to 16.5 MHz */
Stefan Taunera6a0d202013-09-15 14:17:39 +0000391
Stefan Tauner21071b02014-05-16 21:39:48 +0000392 char *spispeed = extract_programmer_param("spispeed");
393 if (spispeed != NULL) {
Carl-Daniel Hailfinger57cdd6b2016-03-12 19:49:14 +0000394 unsigned int i;
395 for (i = 0; i < ARRAY_SIZE(spispeeds); i++) {
396 if (strcasecmp(spispeeds[i].name, spispeed) == 0) {
397 spispeed_idx = i;
398 break;
Stefan Tauner21071b02014-05-16 21:39:48 +0000399 }
Stefan Tauner21071b02014-05-16 21:39:48 +0000400 }
Carl-Daniel Hailfinger57cdd6b2016-03-12 19:49:14 +0000401 /* "reserved" is not a valid speed.
402 * Error out on speeds not present in the spispeeds array.
403 * Only Yangtze supports the second half of indices.
404 * No 66 MHz before SB8xx. */
405 if ((strcasecmp(spispeed, "reserved") == 0) ||
406 (i == ARRAY_SIZE(spispeeds)) ||
407 (amd_gen < CHIPSET_YANGTZE && spispeed_idx > 3) ||
408 (amd_gen < CHIPSET_SB89XX && spispeed_idx == 0)) {
Stefan Tauner21071b02014-05-16 21:39:48 +0000409 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
410 free(spispeed);
411 return 1;
412 }
413 free(spispeed);
414 }
415
Stefan Taunera6a0d202013-09-15 14:17:39 +0000416 /* See the chipset support matrix for SPI Base_Addr below for an explanation of the symbols used.
Martin Roth82b6ec12014-07-15 13:50:58 +0000417 * bit 6xx 7xx/SP5100 8xx 9xx hudson1 hudson234 bolton/yangtze
Stefan Taunera6a0d202013-09-15 14:17:39 +0000418 * 18 rsvd <- fastReadEnable ? <- ? SpiReadMode[0]
419 * 29:30 rsvd <- <- ? <- ? SpiReadMode[2:1]
420 */
Martin Roth82b6ec12014-07-15 13:50:58 +0000421 if (amd_gen >= CHIPSET_BOLTON) {
Wei Hu31402ee2014-05-16 21:39:33 +0000422 static const char *spireadmodes[] = {
423 "Normal (up to 33 MHz)", /* 0 */
424 "Reserved", /* 1 */
425 "Dual IO (1-1-2)", /* 2 */
426 "Quad IO (1-1-4)", /* 3 */
427 "Dual IO (1-2-2)", /* 4 */
428 "Quad IO (1-4-4)", /* 5 */
429 "Normal (up to 66 MHz)", /* 6 */
Martin Roth82b6ec12014-07-15 13:50:58 +0000430 "Fast Read", /* 7 (Not defined in the Bolton datasheet.) */
Wei Hu31402ee2014-05-16 21:39:33 +0000431 };
432 tmp = mmio_readl(sb600_spibar + 0x00);
433 uint8_t read_mode = ((tmp >> 28) & 0x6) | ((tmp >> 18) & 0x1);
434 msg_pdbg("SpiReadMode=%s (%i)\n", spireadmodes[read_mode], read_mode);
435 if (read_mode != 6) {
436 read_mode = 6; /* Default to "Normal (up to 66 MHz)" */
437 if (set_mode(dev, read_mode) != 0) {
438 msg_perr("Setting read mode to \"%s\" failed.\n", spireadmodes[read_mode]);
439 return 1;
440 }
441 msg_pdbg("Setting read mode to \"%s\" succeeded.\n", spireadmodes[read_mode]);
442 }
443
Martin Roth82b6ec12014-07-15 13:50:58 +0000444 if (amd_gen >= CHIPSET_YANGTZE) {
445 tmp = mmio_readb(sb600_spibar + 0x20);
446 msg_pdbg("UseSpi100 is %sabled\n", (tmp & 0x1) ? "en" : "dis");
447 if ((tmp & 0x1) == 0) {
448 rmmio_writeb(tmp | 0x1, sb600_spibar + 0x20);
449 tmp = mmio_readb(sb600_spibar + 0x20) & 0x1;
450 if (tmp == 0) {
451 msg_perr("Enabling Spi100 failed.\n");
452 return 1;
453 }
454 msg_pdbg("Enabling Spi100 succeeded.\n");
455 }
456
457 tmp = mmio_readw(sb600_spibar + 0x22); /* SPI 100 Speed Config */
458 msg_pdbg("NormSpeedNew is %s\n", spispeeds[(tmp >> 12) & 0xf].name);
459 msg_pdbg("FastSpeedNew is %s\n", spispeeds[(tmp >> 8) & 0xf].name);
460 msg_pdbg("AltSpeedNew is %s\n", spispeeds[(tmp >> 4) & 0xf].name);
461 msg_pdbg("TpmSpeedNew is %s\n", spispeeds[(tmp >> 0) & 0xf].name);
462 }
Wei Hu31402ee2014-05-16 21:39:33 +0000463 } else {
Stefan Taunera6a0d202013-09-15 14:17:39 +0000464 if (amd_gen >= CHIPSET_SB89XX && amd_gen <= CHIPSET_HUDSON234) {
465 bool fast_read = (mmio_readl(sb600_spibar + 0x00) >> 18) & 0x1;
466 msg_pdbg("Fast Reads are %sabled\n", fast_read ? "en" : "dis");
467 if (fast_read) {
468 msg_pdbg("Disabling them temporarily.\n");
469 rmmio_writel(mmio_readl(sb600_spibar + 0x00) & ~(0x1 << 18),
470 sb600_spibar + 0x00);
471 }
472 }
473 tmp = (mmio_readb(sb600_spibar + 0xd) >> 4) & 0x3;
474 msg_pdbg("NormSpeed is %s\n", spispeeds[tmp].name);
475 }
476 return set_speed(dev, &spispeeds[spispeed_idx]);
477}
478
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000479static int handle_imc(struct pci_dev *dev)
Rudolf Marek70e14592013-07-25 22:58:56 +0000480{
481 /* Handle IMC everywhere but sb600 which does not have one. */
Stefan Tauner4442b812013-09-12 15:48:35 +0000482 if (amd_gen == CHIPSET_SB6XX)
Rudolf Marek70e14592013-07-25 22:58:56 +0000483 return 0;
484
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000485 bool amd_imc_force = false;
486 char *arg = extract_programmer_param("amd_imc_force");
487 if (arg && !strcmp(arg, "yes")) {
488 amd_imc_force = true;
489 msg_pspew("amd_imc_force enabled.\n");
490 } else if (arg && !strlen(arg)) {
491 msg_perr("Missing argument for amd_imc_force.\n");
492 free(arg);
493 return 1;
494 } else if (arg) {
495 msg_perr("Unknown argument for amd_imc_force: \"%s\" (not \"yes\").\n", arg);
496 free(arg);
497 return 1;
498 }
499 free(arg);
500
Rudolf Marek70e14592013-07-25 22:58:56 +0000501 /* TODO: we should not only look at IntegratedImcPresent (LPC Dev 20, Func 3, 40h) but also at
Stefan Tauner5c316f92015-02-08 21:57:52 +0000502 * IMCEnable(Strap) and Override EcEnable(Strap) (sb8xx, sb9xx?, a50, Bolton: Misc_Reg: 80h-87h;
Rudolf Marek70e14592013-07-25 22:58:56 +0000503 * sb7xx, sp5100: PM_Reg: B0h-B1h) etc. */
504 uint8_t reg = pci_read_byte(dev, 0x40);
505 if ((reg & (1 << 7)) == 0) {
506 msg_pdbg("IMC is not active.\n");
507 return 0;
508 }
509
510 if (!amd_imc_force)
511 programmer_may_write = 0;
Stefan Tauner463dd692013-08-08 12:00:19 +0000512 msg_pinfo("Writes have been disabled for safety reasons because the presence of the IMC\n"
513 "was detected and it could interfere with accessing flash memory. Flashrom will\n"
514 "try to disable it temporarily but even then this might not be safe:\n"
Rudolf Marek70e14592013-07-25 22:58:56 +0000515 "when it is reenabled and after a reboot it expects to find working code\n"
516 "in the flash and it is unpredictable what happens if there is none.\n"
517 "\n"
518 "To be safe make sure that there is a working IMC firmware at the right\n"
519 "location in the image you intend to write and do not attempt to erase.\n"
520 "\n"
521 "You can enforce write support with the amd_imc_force programmer option.\n");
522 if (amd_imc_force)
523 msg_pinfo("Continuing with write support because the user forced us to!\n");
524
525 return amd_imc_shutdown(dev);
526}
527
Michael Karcherb05b9e12010-07-22 18:04:19 +0000528int sb600_probe_spi(struct pci_dev *dev)
529{
530 struct pci_dev *smbus_dev;
531 uint32_t tmp;
532 uint8_t reg;
Rudolf Marek70e14592013-07-25 22:58:56 +0000533
Michael Karcherb05b9e12010-07-22 18:04:19 +0000534 /* Read SPI_BaseAddr */
535 tmp = pci_read_long(dev, 0xa0);
536 tmp &= 0xffffffe0; /* remove bits 4-0 (reserved) */
537 msg_pdbg("SPI base address is at 0x%x\n", tmp);
538
539 /* If the BAR has address 0, it is unlikely SPI is used. */
540 if (!tmp)
541 return 0;
542
543 /* Physical memory has to be mapped at page (4k) boundaries. */
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000544 sb600_spibar = rphysmap("SB600 SPI registers", tmp & 0xfffff000, 0x1000);
545 if (sb600_spibar == ERROR_PTR)
Niklas Söderlund5d307202013-09-14 09:02:27 +0000546 return ERROR_FATAL;
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000547
Michael Karcherb05b9e12010-07-22 18:04:19 +0000548 /* The low bits of the SPI base address are used as offset into
549 * the mapped page.
550 */
551 sb600_spibar += tmp & 0xfff;
552
Stefan Tauner463dd692013-08-08 12:00:19 +0000553 determine_generation(dev);
Stefan Tauner4442b812013-09-12 15:48:35 +0000554 if (amd_gen == CHIPSET_AMD_UNKNOWN) {
555 msg_perr("Could not determine chipset generation.");
556 return ERROR_NONFATAL;
557 }
Stefan Tauner463dd692013-08-08 12:00:19 +0000558
Stefan Tauner4442b812013-09-12 15:48:35 +0000559 /* How to read the following table and similar ones in this file:
560 * "?" means we have no datasheet for this chipset generation or it doesn't have any relevant info.
561 * "<-" means the bit/register meaning is identical to the next non-"?" chipset to the left. "<-" thus
562 * never refers to another "?".
563 * If a "?" chipset is between two chipsets with identical meaning, we assume the meaning didn't change
564 * twice in between, i.e. the meaning is unchanged for the "?" chipset. Usually we assume that
565 * succeeding hardware supports the same functionality as its predecessor unless proven different by
566 * tests or documentation, hence "?" will often be implemented equally to "<-".
567 *
568 * Chipset support matrix for SPI Base_Addr (LPC PCI reg 0xa0)
569 * bit 6xx 7xx/SP5100 8xx 9xx hudson1 hudson2+ yangtze
570 * 3 rsvd <- <- ? <- ? RouteTpm2Spi
571 * 2 rsvd AbortEnable rsvd ? <- ? <-
572 * 1 rsvd SpiRomEnable <- ? <- ? <-
573 * 0 rsvd AltSpiCSEnable rsvd ? <- ? <-
574 */
575 if (amd_gen >= CHIPSET_SB7XX) {
576 tmp = pci_read_long(dev, 0xa0);
577 msg_pdbg("SpiRomEnable=%i", (tmp >> 1) & 0x1);
578 if (amd_gen == CHIPSET_SB7XX)
579 msg_pdbg(", AltSpiCSEnable=%i, AbortEnable=%i", tmp & 0x1, (tmp >> 2) & 0x1);
Wei Hu31402ee2014-05-16 21:39:33 +0000580 else if (amd_gen == CHIPSET_YANGTZE)
581 msg_pdbg(", RouteTpm2Sp=%i", (tmp >> 3) & 0x1);
Michael Karcherb05b9e12010-07-22 18:04:19 +0000582
Stefan Tauner4442b812013-09-12 15:48:35 +0000583 tmp = pci_read_byte(dev, 0xba);
584 msg_pdbg(", PrefetchEnSPIFromIMC=%i", (tmp & 0x4) >> 2);
585
586 tmp = pci_read_byte(dev, 0xbb);
587 /* FIXME: Set bit 3,6,7 if not already set.
588 * Set bit 5, otherwise SPI accesses are pointless in LPC mode.
589 * See doc 42413 AMD SB700/710/750 RPR.
590 */
591 if (amd_gen == CHIPSET_SB7XX)
592 msg_pdbg(", SpiOpEnInLpcMode=%i", (tmp >> 5) & 0x1);
593 msg_pdbg(", PrefetchEnSPIFromHost=%i\n", tmp & 0x1);
594 }
595
596 /* Chipset support matrix for SPI_Cntrl0 (spibar + 0x0)
597 * See the chipset support matrix for SPI Base_Addr above for an explanation of the symbols used.
598 * bit 6xx 7xx/SP5100 8xx 9xx hudson1 hudson2+ yangtze
599 * 17 rsvd <- <- ? <- ? <-
Stefan Taunera6a0d202013-09-15 14:17:39 +0000600 * 18 rsvd <- fastReadEnable<1> ? <- ? SpiReadMode[0]<1>
Stefan Tauner4442b812013-09-12 15:48:35 +0000601 * 19 SpiArbEnable <- <- ? <- ? <-
602 * 20 (FifoPtrClr) <- <- ? <- ? <-
603 * 21 (FifoPtrInc) <- <- ? <- ? IllegalAccess
604 * 22 SpiAccessMacRomEn <- <- ? <- ? <-
605 * 23 SpiHostAccessRomEn <- <- ? <- ? <-
606 * 24:26 ArbWaitCount <- <- ? <- ? <-
607 * 27 SpiBridgeDisable <- <- ? <- ? rsvd
608 * 28 rsvd DropOneClkOnRd = SPIClkGate ? <- ? <-
Stefan Taunera6a0d202013-09-15 14:17:39 +0000609 * 29:30 rsvd <- <- ? <- ? SpiReadMode[2:1]<1>
Stefan Tauner4442b812013-09-12 15:48:35 +0000610 * 31 rsvd <- SpiBusy ? <- ? <-
Stefan Taunera6a0d202013-09-15 14:17:39 +0000611 *
612 * <1> see handle_speed
Carl-Daniel Hailfingereb0e7fc2010-08-18 15:12:43 +0000613 */
Stefan Tauner4442b812013-09-12 15:48:35 +0000614 tmp = mmio_readl(sb600_spibar + 0x00);
615 msg_pdbg("(0x%08" PRIx32 ") SpiArbEnable=%i", tmp, (tmp >> 19) & 0x1);
Wei Hu31402ee2014-05-16 21:39:33 +0000616 if (amd_gen == CHIPSET_YANGTZE)
617 msg_pdbg(", IllegalAccess=%i", (tmp >> 21) & 0x1);
Stefan Tauner4442b812013-09-12 15:48:35 +0000618
619 msg_pdbg(", SpiAccessMacRomEn=%i, SpiHostAccessRomEn=%i, ArbWaitCount=%i",
620 (tmp >> 22) & 0x1, (tmp >> 23) & 0x1, (tmp >> 24) & 0x7);
621
622 if (amd_gen != CHIPSET_YANGTZE)
623 msg_pdbg(", SpiBridgeDisable=%i", (tmp >> 27) & 0x1);
624
625 switch (amd_gen) {
626 case CHIPSET_SB7XX:
627 msg_pdbg(", DropOneClkOnRd/SpiClkGate=%i", (tmp >> 28) & 0x1);
628 case CHIPSET_SB89XX:
629 case CHIPSET_HUDSON234:
Wei Hu31402ee2014-05-16 21:39:33 +0000630 case CHIPSET_YANGTZE:
Stefan Tauner4442b812013-09-12 15:48:35 +0000631 msg_pdbg(", SpiBusy=%i", (tmp >> 31) & 0x1);
632 default: break;
633 }
634 msg_pdbg("\n");
635
636 if (((tmp >> 22) & 0x1) == 0 || ((tmp >> 23) & 0x1) == 0) {
637 msg_perr("ERROR: State of SpiAccessMacRomEn or SpiHostAccessRomEn prohibits full access.\n");
638 return ERROR_NONFATAL;
639 }
640
Stefan Tauner4442b812013-09-12 15:48:35 +0000641 if (amd_gen >= CHIPSET_SB89XX) {
642 tmp = mmio_readb(sb600_spibar + 0x1D);
643 msg_pdbg("Using SPI_CS%d\n", tmp & 0x3);
Wei Hu31402ee2014-05-16 21:39:33 +0000644 /* FIXME: Handle SpiProtect* configuration on Yangtze. */
Stefan Tauner4442b812013-09-12 15:48:35 +0000645 }
646
Michael Karcherb05b9e12010-07-22 18:04:19 +0000647 /* Look for the SMBus device. */
648 smbus_dev = pci_dev_find(0x1002, 0x4385);
Michael Karcherb05b9e12010-07-22 18:04:19 +0000649 if (!smbus_dev) {
Stefan Tauner463dd692013-08-08 12:00:19 +0000650 smbus_dev = pci_dev_find(0x1022, 0x780b); /* AMD FCH */
Wang Qing Pei6e9e2ee2011-08-26 21:11:41 +0000651 if (!smbus_dev) {
652 msg_perr("ERROR: SMBus device not found. Not enabling SPI.\n");
653 return ERROR_NONFATAL;
654 }
Michael Karcherb05b9e12010-07-22 18:04:19 +0000655 }
656
657 /* Note about the bit tests below: If a bit is zero, the GPIO is SPI. */
658 /* GPIO11/SPI_DO and GPIO12/SPI_DI status */
659 reg = pci_read_byte(smbus_dev, 0xAB);
660 reg &= 0xC0;
661 msg_pdbg("GPIO11 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_DO");
662 msg_pdbg("GPIO12 used for %s\n", (reg & (1 << 7)) ? "GPIO" : "SPI_DI");
663 if (reg != 0x00) {
664 msg_pdbg("Not enabling SPI");
665 return 0;
666 }
667 /* GPIO31/SPI_HOLD and GPIO32/SPI_CS status */
668 reg = pci_read_byte(smbus_dev, 0x83);
669 reg &= 0xC0;
670 msg_pdbg("GPIO31 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_HOLD");
671 msg_pdbg("GPIO32 used for %s\n", (reg & (1 << 7)) ? "GPIO" : "SPI_CS");
672 /* SPI_HOLD is not used on all boards, filter it out. */
673 if ((reg & 0x80) != 0x00) {
674 msg_pdbg("Not enabling SPI");
675 return 0;
676 }
677 /* GPIO47/SPI_CLK status */
678 reg = pci_read_byte(smbus_dev, 0xA7);
679 reg &= 0x40;
680 msg_pdbg("GPIO47 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_CLK");
681 if (reg != 0x00) {
682 msg_pdbg("Not enabling SPI");
683 return 0;
684 }
685
Stefan Taunera6a0d202013-09-15 14:17:39 +0000686 if (handle_speed(dev) != 0)
687 return ERROR_FATAL;
688
Stefan Taunerd5b2aef2014-05-16 21:39:28 +0000689 if (handle_imc(dev) != 0)
Rudolf Marek70e14592013-07-25 22:58:56 +0000690 return ERROR_FATAL;
Carl-Daniel Hailfinger39446e32010-09-15 12:02:07 +0000691
Wei Hu31402ee2014-05-16 21:39:33 +0000692 /* Starting with Yangtze the SPI controller got a different interface with a much bigger buffer. */
693 if (amd_gen != CHIPSET_YANGTZE)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000694 register_spi_master(&spi_master_sb600);
Wei Hu31402ee2014-05-16 21:39:33 +0000695 else
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000696 register_spi_master(&spi_master_yangtze);
Michael Karcherb05b9e12010-07-22 18:04:19 +0000697 return 0;
698}
699
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000700#endif