Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
| 5 | * Copyright (C) 2010 Idwer Vollering |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | /* |
Bill Paul | bf8ea49 | 2014-03-17 22:07:29 +0000 | [diff] [blame] | 22 | * Datasheets: |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 23 | * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual |
| 24 | * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx |
Bill Paul | bf8ea49 | 2014-03-17 22:07:29 +0000 | [diff] [blame] | 25 | * http://www.intel.com/content/www/us/en/ethernet-controllers/pci-pci-x-family-gbe-controllers-software-dev-manual.html |
| 26 | * |
| 27 | * PCIe GbE Controllers Open Source Software Developer's Manual |
| 28 | * http://www.intel.com/content/www/us/en/ethernet-controllers/pcie-gbe-controllers-open-source-manual.html |
| 29 | * |
| 30 | * Intel 82574 Gigabit Ethernet Controller Family Datasheet |
| 31 | * http://www.intel.com/content/www/us/en/ethernet-controllers/82574l-gbe-controller-datasheet.html |
Ed Swierk | 33180df | 2014-12-05 22:56:13 +0000 | [diff] [blame] | 32 | * |
| 33 | * Intel 82599 10 GbE Controller Datasheet (331520) |
| 34 | * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/82599-10-gbe-controller-datasheet.pdf |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | #include <stdlib.h> |
Stefan Tauner | 6745d6f | 2012-08-26 21:50:36 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 39 | #include "flash.h" |
| 40 | #include "programmer.h" |
Patrick Georgi | 32508eb | 2012-07-20 20:35:14 +0000 | [diff] [blame] | 41 | #include "hwaccess.h" |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 42 | |
| 43 | #define PCI_VENDOR_ID_INTEL 0x8086 |
Stefan Tauner | 6745d6f | 2012-08-26 21:50:36 +0000 | [diff] [blame] | 44 | #define MEMMAP_SIZE getpagesize() |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 45 | |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 46 | /* EEPROM/Flash Control & Data Register */ |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 47 | #define EECD 0x10 |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 48 | /* Flash Access Register */ |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 49 | #define FLA 0x1c |
| 50 | |
| 51 | /* |
| 52 | * Register bits of EECD. |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 53 | * Table 13-6 |
| 54 | * |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 55 | * Bit 04, 05: FWE (Flash Write Enable Control) |
Ed Swierk | 33180df | 2014-12-05 22:56:13 +0000 | [diff] [blame] | 56 | * 00b = not allowed (on some cards this sends an erase command if bit 31 (FL_ER) of FLA is set) |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 57 | * 01b = flash writes disabled |
| 58 | * 10b = flash writes enabled |
| 59 | * 11b = not allowed |
| 60 | */ |
| 61 | #define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */ |
| 62 | #define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */ |
| 63 | |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 64 | /* Flash Access register bits |
| 65 | * Table 13-9 |
| 66 | */ |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 67 | #define FL_SCK 0 |
| 68 | #define FL_CS 1 |
| 69 | #define FL_SI 2 |
| 70 | #define FL_SO 3 |
| 71 | #define FL_REQ 4 |
| 72 | #define FL_GNT 5 |
Ricardo Ribalda Delgado | 26d33d2 | 2017-03-22 14:30:52 +0100 | [diff] [blame] | 73 | #define FL_LOCKED 6 |
| 74 | #define FL_ABORT 7 |
| 75 | #define FL_CLR_ERR 8 |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 76 | /* Currently unused */ |
| 77 | // #define FL_BUSY 30 |
| 78 | // #define FL_ER 31 |
| 79 | |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 80 | #define BIT(x) (1<<(x)) |
| 81 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 82 | uint8_t *nicintel_spibar; |
| 83 | |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 84 | const struct dev_entry nics_intel_spi[] = { |
Idwer Vollering | bdc4827 | 2010-10-05 11:16:14 +0000 | [diff] [blame] | 85 | {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"}, |
Stefan Tauner | 4b90e6b | 2011-05-18 01:31:24 +0000 | [diff] [blame] | 86 | {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"}, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 87 | {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"}, |
Idwer Vollering | bdc4827 | 2010-10-05 11:16:14 +0000 | [diff] [blame] | 88 | {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"}, |
Bill Paul | bf8ea49 | 2014-03-17 22:07:29 +0000 | [diff] [blame] | 89 | {PCI_VENDOR_ID_INTEL, 0x10d3, OK, "Intel", "82574L Gigabit Ethernet Controller"}, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 90 | |
Ed Swierk | 33180df | 2014-12-05 22:56:13 +0000 | [diff] [blame] | 91 | {PCI_VENDOR_ID_INTEL, 0x10d8, NT, "Intel", "82599 10 Gigabit Unprogrammed Network Controller"}, |
| 92 | {PCI_VENDOR_ID_INTEL, 0x10f7, NT, "Intel", "82599 10 Gigabit KX4 Dual Port Network Controller"}, |
| 93 | {PCI_VENDOR_ID_INTEL, 0x10f8, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller"}, |
| 94 | {PCI_VENDOR_ID_INTEL, 0x10f9, NT, "Intel", "82599 10 Gigabit CX4 Dual Port Network Controller"}, |
| 95 | {PCI_VENDOR_ID_INTEL, 0x10fb, NT, "Intel", "82599 10-Gigabit SFI/SFP+ Network Controller"}, |
| 96 | {PCI_VENDOR_ID_INTEL, 0x10fc, OK, "Intel", "82599 10 Gigabit XAUI/BX4 Dual Port Network Controller"}, |
| 97 | {PCI_VENDOR_ID_INTEL, 0x1517, NT, "Intel", "82599 10 Gigabit KR Network Controller"}, |
| 98 | {PCI_VENDOR_ID_INTEL, 0x151c, NT, "Intel", "82599 10 Gigabit TN Network Controller"}, |
| 99 | {PCI_VENDOR_ID_INTEL, 0x1529, NT, "Intel", "82599 10 Gigabit Dual Port Network Controller with FCoE"}, |
| 100 | {PCI_VENDOR_ID_INTEL, 0x152a, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller with FCoE"}, |
| 101 | {PCI_VENDOR_ID_INTEL, 0x1557, NT, "Intel", "82599 10 Gigabit SFI Network Controller"}, |
| 102 | |
Ricardo Ribalda Delgado | 26d33d2 | 2017-03-22 14:30:52 +0100 | [diff] [blame] | 103 | {PCI_VENDOR_ID_INTEL, 0x1531, OK, "Intel", "I210 Gigabit Network Connection Unprogrammed"}, |
| 104 | {PCI_VENDOR_ID_INTEL, 0x1532, NT, "Intel", "I211 Gigabit Network Connection Unprogrammed"}, |
| 105 | {PCI_VENDOR_ID_INTEL, 0x1533, NT, "Intel", "I210 Gigabit Network Connection"}, |
| 106 | {PCI_VENDOR_ID_INTEL, 0x1536, NT, "Intel", "I210 Gigabit Network Connection SERDES Fiber"}, |
| 107 | {PCI_VENDOR_ID_INTEL, 0x1537, NT, "Intel", "I210 Gigabit Network Connection SERDES Backplane"}, |
| 108 | {PCI_VENDOR_ID_INTEL, 0x1538, NT, "Intel", "I210 Gigabit Network Connection SGMII"}, |
| 109 | {PCI_VENDOR_ID_INTEL, 0x1539, NT, "Intel", "I211 Gigabit Network Connection"}, |
| 110 | |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 111 | {0}, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static void nicintel_request_spibus(void) |
| 115 | { |
| 116 | uint32_t tmp; |
| 117 | |
| 118 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 119 | tmp |= BIT(FL_REQ); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 120 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 121 | |
| 122 | /* Wait until we are allowed to use the SPI bus. */ |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 123 | while (!(pci_mmio_readl(nicintel_spibar + FLA) & BIT(FL_GNT))) ; |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void nicintel_release_spibus(void) |
| 127 | { |
| 128 | uint32_t tmp; |
| 129 | |
| 130 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 131 | tmp &= ~BIT(FL_REQ); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 132 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 133 | } |
| 134 | |
| 135 | static void nicintel_bitbang_set_cs(int val) |
| 136 | { |
| 137 | uint32_t tmp; |
| 138 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 139 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 140 | tmp &= ~BIT(FL_CS); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 141 | tmp |= (val << FL_CS); |
| 142 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void nicintel_bitbang_set_sck(int val) |
| 146 | { |
| 147 | uint32_t tmp; |
| 148 | |
| 149 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 150 | tmp &= ~BIT(FL_SCK); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 151 | tmp |= (val << FL_SCK); |
| 152 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 153 | } |
| 154 | |
| 155 | static void nicintel_bitbang_set_mosi(int val) |
| 156 | { |
| 157 | uint32_t tmp; |
| 158 | |
| 159 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 160 | tmp &= ~BIT(FL_SI); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 161 | tmp |= (val << FL_SI); |
| 162 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 163 | } |
| 164 | |
| 165 | static int nicintel_bitbang_get_miso(void) |
| 166 | { |
| 167 | uint32_t tmp; |
| 168 | |
| 169 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 170 | tmp = (tmp >> FL_SO) & 0x1; |
| 171 | return tmp; |
| 172 | } |
| 173 | |
| 174 | static const struct bitbang_spi_master bitbang_spi_master_nicintel = { |
| 175 | .type = BITBANG_SPI_MASTER_NICINTEL, |
| 176 | .set_cs = nicintel_bitbang_set_cs, |
| 177 | .set_sck = nicintel_bitbang_set_sck, |
| 178 | .set_mosi = nicintel_bitbang_set_mosi, |
| 179 | .get_miso = nicintel_bitbang_get_miso, |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 180 | .request_bus = nicintel_request_spibus, |
| 181 | .release_bus = nicintel_release_spibus, |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 182 | .half_period = 1, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 185 | static int nicintel_spi_shutdown(void *data) |
| 186 | { |
| 187 | uint32_t tmp; |
| 188 | |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 189 | /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 190 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 191 | tmp &= ~FLASH_WRITES_ENABLED; |
| 192 | tmp |= FLASH_WRITES_DISABLED; |
| 193 | pci_mmio_writel(tmp, nicintel_spibar + EECD); |
| 194 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
Ricardo Ribalda Delgado | 26d33d2 | 2017-03-22 14:30:52 +0100 | [diff] [blame] | 198 | static int nicintel_spi_82599_enable_flash(void) |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 199 | { |
| 200 | uint32_t tmp; |
| 201 | |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 202 | /* Automatic restore of EECD on shutdown is not possible because EECD |
| 203 | * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED, |
| 204 | * but other bits with side effects as well. Those other bits must be |
| 205 | * left untouched. |
| 206 | */ |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 207 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 208 | tmp &= ~FLASH_WRITES_DISABLED; |
| 209 | tmp |= FLASH_WRITES_ENABLED; |
| 210 | pci_mmio_writel(tmp, nicintel_spibar + EECD); |
| 211 | |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 212 | /* test if FWE is really set to allow writes */ |
| 213 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 214 | if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) { |
| 215 | msg_perr("Enabling flash write access failed.\n"); |
| 216 | return 1; |
| 217 | } |
| 218 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 219 | if (register_shutdown(nicintel_spi_shutdown, NULL)) |
| 220 | return 1; |
| 221 | |
Ricardo Ribalda Delgado | 26d33d2 | 2017-03-22 14:30:52 +0100 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int nicintel_spi_i210_enable_flash() |
| 226 | { |
| 227 | uint32_t tmp; |
| 228 | |
| 229 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 230 | if (tmp & BIT(FL_LOCKED)) { |
Ricardo Ribalda Delgado | 26d33d2 | 2017-03-22 14:30:52 +0100 | [diff] [blame] | 231 | msg_perr("Flash is in Secure Mode. Abort.\n"); |
| 232 | return 1; |
| 233 | } |
| 234 | |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 235 | if (!(tmp & BIT(FL_ABORT))) |
Ricardo Ribalda Delgado | 26d33d2 | 2017-03-22 14:30:52 +0100 | [diff] [blame] | 236 | return 0; |
| 237 | |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 238 | tmp |= BIT(FL_CLR_ERR); |
Ricardo Ribalda Delgado | 26d33d2 | 2017-03-22 14:30:52 +0100 | [diff] [blame] | 239 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 240 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Ricardo Ribalda Delgado | 75a2a79 | 2017-03-23 23:38:04 +0100 | [diff] [blame] | 241 | if (!(tmp & BIT(FL_ABORT))) { |
Ricardo Ribalda Delgado | 26d33d2 | 2017-03-22 14:30:52 +0100 | [diff] [blame] | 242 | msg_perr("Unable to clear Flash Access Error. Abort\n"); |
| 243 | return 1; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | int nicintel_spi_init(void) |
| 250 | { |
| 251 | struct pci_dev *dev = NULL; |
| 252 | |
| 253 | if (rget_io_perms()) |
| 254 | return 1; |
| 255 | |
| 256 | dev = pcidev_init(nics_intel_spi, PCI_BASE_ADDRESS_0); |
| 257 | if (!dev) |
| 258 | return 1; |
| 259 | |
| 260 | uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0); |
| 261 | if (!io_base_addr) |
| 262 | return 1; |
| 263 | |
| 264 | if ((dev->device_id & 0xfff0) == 0x1530) { |
| 265 | nicintel_spibar = rphysmap("Intel I210 Gigabit w/ SPI flash", io_base_addr + 0x12000, |
| 266 | MEMMAP_SIZE); |
| 267 | if (!nicintel_spibar || nicintel_spi_i210_enable_flash()) |
| 268 | return 1; |
| 269 | } else if (dev->device_id < 0x10d8) { |
| 270 | nicintel_spibar = rphysmap("Intel Gigabit NIC w/ SPI flash", io_base_addr, |
| 271 | MEMMAP_SIZE); |
| 272 | if (!nicintel_spibar || nicintel_spi_82599_enable_flash()) |
| 273 | return 1; |
| 274 | } else { |
| 275 | nicintel_spibar = rphysmap("Intel 10 Gigabit NIC w/ SPI flash", io_base_addr + 0x10000, |
| 276 | MEMMAP_SIZE); |
| 277 | if (!nicintel_spibar || nicintel_spi_82599_enable_flash()) |
| 278 | return 1; |
| 279 | } |
| 280 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 281 | if (register_spi_bitbang_master(&bitbang_spi_master_nicintel)) |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 282 | return 1; |
| 283 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 284 | return 0; |
| 285 | } |