uwe | 6764e92 | 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. |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
Patrick Georgi | e39d644 | 2017-03-22 21:23:35 +0100 | [diff] [blame] | 18 | * Datasheets: |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 19 | * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual |
| 20 | * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx |
Patrick Georgi | e39d644 | 2017-03-22 21:23:35 +0100 | [diff] [blame] | 21 | * http://www.intel.com/content/www/us/en/ethernet-controllers/pci-pci-x-family-gbe-controllers-software-dev-manual.html |
| 22 | * |
| 23 | * PCIe GbE Controllers Open Source Software Developer's Manual |
| 24 | * http://www.intel.com/content/www/us/en/ethernet-controllers/pcie-gbe-controllers-open-source-manual.html |
| 25 | * |
| 26 | * Intel 82574 Gigabit Ethernet Controller Family Datasheet |
| 27 | * http://www.intel.com/content/www/us/en/ethernet-controllers/82574l-gbe-controller-datasheet.html |
| 28 | * |
| 29 | * Intel 82599 10 GbE Controller Datasheet (331520) |
| 30 | * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/82599-10-gbe-controller-datasheet.pdf |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include <stdlib.h> |
Patrick Georgi | 5dc3d88 | 2017-03-31 16:57:08 +0200 | [diff] [blame] | 34 | #include <unistd.h> |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 35 | #include "flash.h" |
| 36 | #include "programmer.h" |
Patrick Georgi | 5dc3d88 | 2017-03-31 16:57:08 +0200 | [diff] [blame] | 37 | #include "hwaccess.h" |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 38 | |
| 39 | #define PCI_VENDOR_ID_INTEL 0x8086 |
Patrick Georgi | 5dc3d88 | 2017-03-31 16:57:08 +0200 | [diff] [blame] | 40 | #define MEMMAP_SIZE getpagesize() |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 41 | |
Patrick Georgi | e39d644 | 2017-03-22 21:23:35 +0100 | [diff] [blame] | 42 | /* EEPROM/Flash Control & Data Register */ |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 43 | #define EECD 0x10 |
Patrick Georgi | e39d644 | 2017-03-22 21:23:35 +0100 | [diff] [blame] | 44 | /* Flash Access Register */ |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 45 | #define FLA 0x1c |
| 46 | |
| 47 | /* |
| 48 | * Register bits of EECD. |
Patrick Georgi | e39d644 | 2017-03-22 21:23:35 +0100 | [diff] [blame] | 49 | * Table 13-6 |
| 50 | * |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 51 | * Bit 04, 05: FWE (Flash Write Enable Control) |
Patrick Georgi | e39d644 | 2017-03-22 21:23:35 +0100 | [diff] [blame] | 52 | * 00b = not allowed (on some cards this sends an erase command if bit 31 (FL_ER) of FLA is set) |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 53 | * 01b = flash writes disabled |
| 54 | * 10b = flash writes enabled |
| 55 | * 11b = not allowed |
| 56 | */ |
| 57 | #define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */ |
| 58 | #define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */ |
| 59 | |
Patrick Georgi | e39d644 | 2017-03-22 21:23:35 +0100 | [diff] [blame] | 60 | /* Flash Access register bits |
| 61 | * Table 13-9 |
| 62 | */ |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 63 | #define FL_SCK 0 |
| 64 | #define FL_CS 1 |
| 65 | #define FL_SI 2 |
| 66 | #define FL_SO 3 |
| 67 | #define FL_REQ 4 |
| 68 | #define FL_GNT 5 |
| 69 | /* Currently unused */ |
| 70 | // #define FL_BUSY 30 |
| 71 | // #define FL_ER 31 |
| 72 | |
Edward O'Callaghan | 1aa8e9a | 2019-08-02 17:16:47 +1000 | [diff] [blame^] | 73 | #define BIT(x) (1<<(x)) |
| 74 | |
Edward O'Callaghan | ef4e28b | 2019-06-28 13:18:41 +1000 | [diff] [blame] | 75 | static uint8_t *nicintel_spibar; |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 76 | |
Patrick Georgi | 8ae1657 | 2017-03-09 15:59:25 +0100 | [diff] [blame] | 77 | const struct dev_entry nics_intel_spi[] = { |
hailfinger | 14ef311 | 2010-10-05 11:16:14 +0000 | [diff] [blame] | 78 | {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"}, |
stefanct | c79b8b1 | 2011-05-18 01:31:24 +0000 | [diff] [blame] | 79 | {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"}, |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 80 | {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"}, |
hailfinger | 14ef311 | 2010-10-05 11:16:14 +0000 | [diff] [blame] | 81 | {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"}, |
Patrick Georgi | 5dc3d88 | 2017-03-31 16:57:08 +0200 | [diff] [blame] | 82 | {PCI_VENDOR_ID_INTEL, 0x10d3, OK, "Intel", "82574L Gigabit Ethernet Controller"}, |
| 83 | |
| 84 | {PCI_VENDOR_ID_INTEL, 0x10d8, NT, "Intel", "82599 10 Gigabit Unprogrammed Network Controller"}, |
| 85 | {PCI_VENDOR_ID_INTEL, 0x10f7, NT, "Intel", "82599 10 Gigabit KX4 Dual Port Network Controller"}, |
| 86 | {PCI_VENDOR_ID_INTEL, 0x10f8, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller"}, |
| 87 | {PCI_VENDOR_ID_INTEL, 0x10f9, NT, "Intel", "82599 10 Gigabit CX4 Dual Port Network Controller"}, |
| 88 | {PCI_VENDOR_ID_INTEL, 0x10fb, NT, "Intel", "82599 10-Gigabit SFI/SFP+ Network Controller"}, |
| 89 | {PCI_VENDOR_ID_INTEL, 0x10fc, OK, "Intel", "82599 10 Gigabit XAUI/BX4 Dual Port Network Controller"}, |
| 90 | {PCI_VENDOR_ID_INTEL, 0x1517, NT, "Intel", "82599 10 Gigabit KR Network Controller"}, |
| 91 | {PCI_VENDOR_ID_INTEL, 0x151c, NT, "Intel", "82599 10 Gigabit TN Network Controller"}, |
| 92 | {PCI_VENDOR_ID_INTEL, 0x1529, NT, "Intel", "82599 10 Gigabit Dual Port Network Controller with FCoE"}, |
| 93 | {PCI_VENDOR_ID_INTEL, 0x152a, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller with FCoE"}, |
| 94 | {PCI_VENDOR_ID_INTEL, 0x1557, NT, "Intel", "82599 10 Gigabit SFI Network Controller"}, |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 95 | |
Patrick Georgi | 8ddfee9 | 2017-03-20 14:54:28 +0100 | [diff] [blame] | 96 | {0}, |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static void nicintel_request_spibus(void) |
| 100 | { |
| 101 | uint32_t tmp; |
| 102 | |
| 103 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Edward O'Callaghan | 1aa8e9a | 2019-08-02 17:16:47 +1000 | [diff] [blame^] | 104 | tmp |= BIT(FL_REQ); |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 105 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 106 | |
| 107 | /* Wait until we are allowed to use the SPI bus. */ |
Edward O'Callaghan | 1aa8e9a | 2019-08-02 17:16:47 +1000 | [diff] [blame^] | 108 | while (!(pci_mmio_readl(nicintel_spibar + FLA) & BIT(FL_GNT))) ; |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void nicintel_release_spibus(void) |
| 112 | { |
| 113 | uint32_t tmp; |
| 114 | |
| 115 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Edward O'Callaghan | 1aa8e9a | 2019-08-02 17:16:47 +1000 | [diff] [blame^] | 116 | tmp &= ~BIT(FL_REQ); |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 117 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 118 | } |
| 119 | |
| 120 | static void nicintel_bitbang_set_cs(int val) |
| 121 | { |
| 122 | uint32_t tmp; |
| 123 | |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 124 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Edward O'Callaghan | 1aa8e9a | 2019-08-02 17:16:47 +1000 | [diff] [blame^] | 125 | tmp &= ~BIT(FL_CS); |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 126 | tmp |= (val << FL_CS); |
| 127 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void nicintel_bitbang_set_sck(int val) |
| 131 | { |
| 132 | uint32_t tmp; |
| 133 | |
| 134 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Edward O'Callaghan | 1aa8e9a | 2019-08-02 17:16:47 +1000 | [diff] [blame^] | 135 | tmp &= ~BIT(FL_SCK); |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 136 | tmp |= (val << FL_SCK); |
| 137 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 138 | } |
| 139 | |
| 140 | static void nicintel_bitbang_set_mosi(int val) |
| 141 | { |
| 142 | uint32_t tmp; |
| 143 | |
| 144 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
Edward O'Callaghan | 1aa8e9a | 2019-08-02 17:16:47 +1000 | [diff] [blame^] | 145 | tmp &= ~BIT(FL_SI); |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 146 | tmp |= (val << FL_SI); |
| 147 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 148 | } |
| 149 | |
| 150 | static int nicintel_bitbang_get_miso(void) |
| 151 | { |
| 152 | uint32_t tmp; |
| 153 | |
| 154 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 155 | tmp = (tmp >> FL_SO) & 0x1; |
| 156 | return tmp; |
| 157 | } |
| 158 | |
| 159 | static const struct bitbang_spi_master bitbang_spi_master_nicintel = { |
| 160 | .type = BITBANG_SPI_MASTER_NICINTEL, |
| 161 | .set_cs = nicintel_bitbang_set_cs, |
| 162 | .set_sck = nicintel_bitbang_set_sck, |
| 163 | .set_mosi = nicintel_bitbang_set_mosi, |
| 164 | .get_miso = nicintel_bitbang_get_miso, |
hailfinger | 12cba9a | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 165 | .request_bus = nicintel_request_spibus, |
| 166 | .release_bus = nicintel_release_spibus, |
Patrick Georgi | e081d5d | 2017-03-22 21:18:18 +0100 | [diff] [blame] | 167 | .half_period = 1, |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 170 | static int nicintel_spi_shutdown(void *data) |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 171 | { |
| 172 | uint32_t tmp; |
| 173 | |
Patrick Georgi | e39d644 | 2017-03-22 21:23:35 +0100 | [diff] [blame] | 174 | /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */ |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 175 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 176 | tmp &= ~FLASH_WRITES_ENABLED; |
| 177 | tmp |= FLASH_WRITES_DISABLED; |
| 178 | pci_mmio_writel(tmp, nicintel_spibar + EECD); |
| 179 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 183 | int nicintel_spi_init(void) |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 184 | { |
Patrick Georgi | 7c30fa9 | 2017-03-28 22:47:12 +0200 | [diff] [blame] | 185 | struct pci_dev *dev = NULL; |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 186 | uint32_t tmp; |
| 187 | |
Patrick Georgi | 2a2d67f | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 188 | if (rget_io_perms()) |
| 189 | return 1; |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 190 | |
Patrick Georgi | 7c30fa9 | 2017-03-28 22:47:12 +0200 | [diff] [blame] | 191 | dev = pcidev_init(nics_intel_spi, PCI_BASE_ADDRESS_0); |
| 192 | if (!dev) |
| 193 | return 1; |
| 194 | |
| 195 | uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0); |
| 196 | if (!io_base_addr) |
| 197 | return 1; |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 198 | |
Patrick Georgi | 5dc3d88 | 2017-03-31 16:57:08 +0200 | [diff] [blame] | 199 | if (dev->device_id < 0x10d8) { |
| 200 | nicintel_spibar = rphysmap("Intel Gigabit NIC w/ SPI flash", io_base_addr, |
| 201 | MEMMAP_SIZE); |
| 202 | } else { |
| 203 | nicintel_spibar = rphysmap("Intel 10 Gigabit NIC w/ SPI flash", io_base_addr + 0x10000, |
| 204 | MEMMAP_SIZE); |
| 205 | } |
| 206 | if (nicintel_spibar == ERROR_PTR) |
| 207 | return 1; |
| 208 | |
hailfinger | 1e2e344 | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 209 | /* Automatic restore of EECD on shutdown is not possible because EECD |
| 210 | * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED, |
| 211 | * but other bits with side effects as well. Those other bits must be |
| 212 | * left untouched. |
| 213 | */ |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 214 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 215 | tmp &= ~FLASH_WRITES_DISABLED; |
| 216 | tmp |= FLASH_WRITES_ENABLED; |
| 217 | pci_mmio_writel(tmp, nicintel_spibar + EECD); |
| 218 | |
Patrick Georgi | 5dc3d88 | 2017-03-31 16:57:08 +0200 | [diff] [blame] | 219 | /* test if FWE is really set to allow writes */ |
| 220 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 221 | if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) { |
| 222 | msg_perr("Enabling flash write access failed.\n"); |
| 223 | return 1; |
| 224 | } |
| 225 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 226 | if (register_shutdown(nicintel_spi_shutdown, NULL)) |
| 227 | return 1; |
| 228 | |
Craig Hesling | 65eb881 | 2019-08-01 09:33:56 -0700 | [diff] [blame] | 229 | if (register_spi_bitbang_master(&bitbang_spi_master_nicintel)) |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 230 | return 1; |
| 231 | |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 232 | return 0; |
| 233 | } |