Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Jason Wang | 13f98ce | 2008-11-29 15:07:15 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008 Wang Qingpei <Qingpei.Wang@amd.com> |
| 5 | * Copyright (C) 2008 Joe Bao <Zheng.Bao@amd.com> |
Uwe Hermann | 97e8f22 | 2009-04-13 21:35:49 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 Advanced Micro Devices, Inc. |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 7 | * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | */ |
| 23 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 24 | #if defined(__i386__) || defined(__x86_64__) |
| 25 | |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 26 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 27 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 28 | #include "programmer.h" |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 29 | #include "spi.h" |
| 30 | |
Carl-Daniel Hailfinger | 2c7ba8c | 2009-06-23 00:47:26 +0000 | [diff] [blame] | 31 | /* This struct is unused, but helps visualize the SB600 SPI BAR layout. |
| 32 | *struct sb600_spi_controller { |
| 33 | * unsigned int spi_cntrl0; / * 00h * / |
| 34 | * unsigned int restrictedcmd1; / * 04h * / |
| 35 | * unsigned int restrictedcmd2; / * 08h * / |
| 36 | * unsigned int spi_cntrl1; / * 0ch * / |
| 37 | * unsigned int spi_cmdvalue0; / * 10h * / |
| 38 | * unsigned int spi_cmdvalue1; / * 14h * / |
| 39 | * unsigned int spi_cmdvalue2; / * 18h * / |
| 40 | * unsigned int spi_fakeid; / * 1Ch * / |
| 41 | *}; |
| 42 | */ |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 43 | |
Michael Karcher | b05b9e1 | 2010-07-22 18:04:19 +0000 | [diff] [blame] | 44 | static uint8_t *sb600_spibar = NULL; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 45 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 46 | int sb600_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 47 | { |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 48 | /* Maximum read length is 8 bytes. */ |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 49 | return spi_read_chunked(flash, buf, start, len, 8); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 52 | int sb600_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 53 | { |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 54 | return spi_write_chunked(flash, buf, start, len, 5); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Carl-Daniel Hailfinger | 2c7ba8c | 2009-06-23 00:47:26 +0000 | [diff] [blame] | 57 | static void reset_internal_fifo_pointer(void) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 58 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 59 | mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 60 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 61 | while (mmio_readb(sb600_spibar + 0xD) & 0x7) |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 62 | msg_pspew("reset\n"); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Carl-Daniel Hailfinger | 2c7ba8c | 2009-06-23 00:47:26 +0000 | [diff] [blame] | 65 | static void execute_command(void) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 66 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 67 | mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 68 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 69 | while (mmio_readb(sb600_spibar + 2) & 1) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 70 | ; |
| 71 | } |
| 72 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 73 | int sb600_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 74 | const unsigned char *writearr, unsigned char *readarr) |
| 75 | { |
| 76 | int count; |
| 77 | /* First byte is cmd which can not being sent through FIFO. */ |
| 78 | unsigned char cmd = *writearr++; |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 79 | unsigned int readoffby1; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 80 | |
| 81 | writecnt--; |
| 82 | |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 83 | msg_pspew("%s, cmd=%x, writecnt=%x, readcnt=%x\n", |
| 84 | __func__, cmd, writecnt, readcnt); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 85 | |
| 86 | if (readcnt > 8) { |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 87 | msg_pinfo("%s, SB600 SPI controller can not receive %d bytes, " |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 88 | "it is limited to 8 bytes\n", __func__, readcnt); |
| 89 | return SPI_INVALID_LENGTH; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | if (writecnt > 8) { |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 93 | msg_pinfo("%s, SB600 SPI controller can not send %d bytes, " |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 94 | "it is limited to 8 bytes\n", __func__, writecnt); |
| 95 | return SPI_INVALID_LENGTH; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 98 | /* This is a workaround for a bug in SB600 and SB700. If we only send |
| 99 | * an opcode and no additional data/address, the SPI controller will |
| 100 | * read one byte too few from the chip. Basically, the last byte of |
| 101 | * the chip response is discarded and will not end up in the FIFO. |
| 102 | * It is unclear if the CS# line is set high too early as well. |
| 103 | */ |
| 104 | readoffby1 = (writecnt) ? 0 : 1; |
| 105 | mmio_writeb((readcnt + readoffby1) << 4 | (writecnt), sb600_spibar + 1); |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 106 | mmio_writeb(cmd, sb600_spibar + 0); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 107 | |
| 108 | /* Before we use the FIFO, reset it first. */ |
| 109 | reset_internal_fifo_pointer(); |
| 110 | |
| 111 | /* Send the write byte to FIFO. */ |
| 112 | for (count = 0; count < writecnt; count++, writearr++) { |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 113 | msg_pspew(" [%x]", *writearr); |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 114 | mmio_writeb(*writearr, sb600_spibar + 0xC); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 115 | } |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 116 | msg_pspew("\n"); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * We should send the data by sequence, which means we need to reset |
| 120 | * the FIFO pointer to the first byte we want to send. |
| 121 | */ |
| 122 | reset_internal_fifo_pointer(); |
| 123 | |
| 124 | execute_command(); |
| 125 | |
| 126 | /* |
| 127 | * After the command executed, we should find out the index of the |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 128 | * received byte. Here we just reset the FIFO pointer and skip the |
| 129 | * writecnt. |
| 130 | * It would be possible to increase the FIFO pointer by one instead |
| 131 | * of reading and discarding one byte from the FIFO. |
| 132 | * The FIFO is implemented on top of an 8 byte ring buffer and the |
| 133 | * buffer is never cleared. For every byte that is shifted out after |
| 134 | * the opcode, the FIFO already stores the response from the chip. |
| 135 | * Usually, the chip will respond with 0x00 or 0xff. |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 136 | */ |
| 137 | reset_internal_fifo_pointer(); |
| 138 | |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 139 | /* Skip the bytes we sent. */ |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 140 | for (count = 0; count < writecnt; count++) { |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 141 | cmd = mmio_readb(sb600_spibar + 0xC); |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 142 | msg_pspew("[ %2x]", cmd); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 145 | msg_pspew("The FIFO pointer after skipping is %d.\n", |
| 146 | mmio_readb(sb600_spibar + 0xd) & 0x07); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 147 | for (count = 0; count < readcnt; count++, readarr++) { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 148 | *readarr = mmio_readb(sb600_spibar + 0xC); |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 149 | msg_pspew("[%02x]", *readarr); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 150 | } |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 151 | msg_pspew("\n"); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 155 | |
Michael Karcher | b05b9e1 | 2010-07-22 18:04:19 +0000 | [diff] [blame] | 156 | int sb600_probe_spi(struct pci_dev *dev) |
| 157 | { |
| 158 | struct pci_dev *smbus_dev; |
| 159 | uint32_t tmp; |
| 160 | uint8_t reg; |
| 161 | /* Read SPI_BaseAddr */ |
| 162 | tmp = pci_read_long(dev, 0xa0); |
| 163 | tmp &= 0xffffffe0; /* remove bits 4-0 (reserved) */ |
| 164 | msg_pdbg("SPI base address is at 0x%x\n", tmp); |
| 165 | |
| 166 | /* If the BAR has address 0, it is unlikely SPI is used. */ |
| 167 | if (!tmp) |
| 168 | return 0; |
| 169 | |
| 170 | /* Physical memory has to be mapped at page (4k) boundaries. */ |
| 171 | sb600_spibar = physmap("SB600 SPI registers", tmp & 0xfffff000, |
| 172 | 0x1000); |
| 173 | /* The low bits of the SPI base address are used as offset into |
| 174 | * the mapped page. |
| 175 | */ |
| 176 | sb600_spibar += tmp & 0xfff; |
| 177 | |
| 178 | tmp = pci_read_long(dev, 0xa0); |
| 179 | msg_pdbg("AltSpiCSEnable=%i, SpiRomEnable=%i, " |
| 180 | "AbortEnable=%i\n", tmp & 0x1, (tmp & 0x2) >> 1, |
| 181 | (tmp & 0x4) >> 2); |
| 182 | tmp = (pci_read_byte(dev, 0xba) & 0x4) >> 2; |
| 183 | msg_pdbg("PrefetchEnSPIFromIMC=%i, ", tmp); |
| 184 | |
| 185 | tmp = pci_read_byte(dev, 0xbb); |
| 186 | msg_pdbg("PrefetchEnSPIFromHost=%i, SpiOpEnInLpcMode=%i\n", |
| 187 | tmp & 0x1, (tmp & 0x20) >> 5); |
| 188 | tmp = mmio_readl(sb600_spibar); |
| 189 | msg_pdbg("SpiArbEnable=%i, SpiAccessMacRomEn=%i, " |
| 190 | "SpiHostAccessRomEn=%i, ArbWaitCount=%i, " |
| 191 | "SpiBridgeDisable=%i, DropOneClkOnRd=%i\n", |
| 192 | (tmp >> 19) & 0x1, (tmp >> 22) & 0x1, |
| 193 | (tmp >> 23) & 0x1, (tmp >> 24) & 0x7, |
| 194 | (tmp >> 27) & 0x1, (tmp >> 28) & 0x1); |
| 195 | |
| 196 | /* Look for the SMBus device. */ |
| 197 | smbus_dev = pci_dev_find(0x1002, 0x4385); |
| 198 | |
| 199 | if (!smbus_dev) { |
| 200 | msg_perr("ERROR: SMBus device not found. Not enabling SPI.\n"); |
| 201 | return ERROR_NONFATAL; |
| 202 | } |
| 203 | |
| 204 | /* Note about the bit tests below: If a bit is zero, the GPIO is SPI. */ |
| 205 | /* GPIO11/SPI_DO and GPIO12/SPI_DI status */ |
| 206 | reg = pci_read_byte(smbus_dev, 0xAB); |
| 207 | reg &= 0xC0; |
| 208 | msg_pdbg("GPIO11 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_DO"); |
| 209 | msg_pdbg("GPIO12 used for %s\n", (reg & (1 << 7)) ? "GPIO" : "SPI_DI"); |
| 210 | if (reg != 0x00) { |
| 211 | msg_pdbg("Not enabling SPI"); |
| 212 | return 0; |
| 213 | } |
| 214 | /* GPIO31/SPI_HOLD and GPIO32/SPI_CS status */ |
| 215 | reg = pci_read_byte(smbus_dev, 0x83); |
| 216 | reg &= 0xC0; |
| 217 | msg_pdbg("GPIO31 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_HOLD"); |
| 218 | msg_pdbg("GPIO32 used for %s\n", (reg & (1 << 7)) ? "GPIO" : "SPI_CS"); |
| 219 | /* SPI_HOLD is not used on all boards, filter it out. */ |
| 220 | if ((reg & 0x80) != 0x00) { |
| 221 | msg_pdbg("Not enabling SPI"); |
| 222 | return 0; |
| 223 | } |
| 224 | /* GPIO47/SPI_CLK status */ |
| 225 | reg = pci_read_byte(smbus_dev, 0xA7); |
| 226 | reg &= 0x40; |
| 227 | msg_pdbg("GPIO47 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_CLK"); |
| 228 | if (reg != 0x00) { |
| 229 | msg_pdbg("Not enabling SPI"); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | buses_supported |= CHIP_BUSTYPE_SPI; |
| 234 | spi_controller = SPI_CONTROLLER_SB600; |
| 235 | return 0; |
| 236 | } |
| 237 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 238 | #endif |