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 | /* |
| 22 | * Datasheet: |
| 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 |
| 25 | * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include "flash.h" |
| 30 | #include "programmer.h" |
Patrick Georgi | 32508eb | 2012-07-20 20:35:14 +0000 | [diff] [blame] | 31 | #include "hwaccess.h" |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 32 | |
| 33 | #define PCI_VENDOR_ID_INTEL 0x8086 |
| 34 | |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 35 | /* EEPROM/Flash Control & Data Register */ |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 36 | #define EECD 0x10 |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 37 | /* Flash Access Register */ |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 38 | #define FLA 0x1c |
| 39 | |
| 40 | /* |
| 41 | * Register bits of EECD. |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 42 | * Table 13-6 |
| 43 | * |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 44 | * Bit 04, 05: FWE (Flash Write Enable Control) |
| 45 | * 00b = not allowed |
| 46 | * 01b = flash writes disabled |
| 47 | * 10b = flash writes enabled |
| 48 | * 11b = not allowed |
| 49 | */ |
| 50 | #define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */ |
| 51 | #define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */ |
| 52 | |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 53 | /* Flash Access register bits |
| 54 | * Table 13-9 |
| 55 | */ |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 56 | #define FL_SCK 0 |
| 57 | #define FL_CS 1 |
| 58 | #define FL_SI 2 |
| 59 | #define FL_SO 3 |
| 60 | #define FL_REQ 4 |
| 61 | #define FL_GNT 5 |
| 62 | /* Currently unused */ |
| 63 | // #define FL_BUSY 30 |
| 64 | // #define FL_ER 31 |
| 65 | |
| 66 | uint8_t *nicintel_spibar; |
| 67 | |
| 68 | const struct pcidev_status nics_intel_spi[] = { |
Idwer Vollering | bdc4827 | 2010-10-05 11:16:14 +0000 | [diff] [blame] | 69 | {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"}, |
Stefan Tauner | 4b90e6b | 2011-05-18 01:31:24 +0000 | [diff] [blame] | 70 | {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"}, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 71 | {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"}, |
Idwer Vollering | bdc4827 | 2010-10-05 11:16:14 +0000 | [diff] [blame] | 72 | {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"}, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 73 | |
| 74 | {}, |
| 75 | }; |
| 76 | |
| 77 | static void nicintel_request_spibus(void) |
| 78 | { |
| 79 | uint32_t tmp; |
| 80 | |
| 81 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 82 | tmp |= 1 << FL_REQ; |
| 83 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 84 | |
| 85 | /* Wait until we are allowed to use the SPI bus. */ |
| 86 | while (!(pci_mmio_readl(nicintel_spibar + FLA) & (1 << FL_GNT))) ; |
| 87 | } |
| 88 | |
| 89 | static void nicintel_release_spibus(void) |
| 90 | { |
| 91 | uint32_t tmp; |
| 92 | |
| 93 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 94 | tmp &= ~(1 << FL_REQ); |
| 95 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 96 | } |
| 97 | |
| 98 | static void nicintel_bitbang_set_cs(int val) |
| 99 | { |
| 100 | uint32_t tmp; |
| 101 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 102 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 103 | tmp &= ~(1 << FL_CS); |
| 104 | tmp |= (val << FL_CS); |
| 105 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void nicintel_bitbang_set_sck(int val) |
| 109 | { |
| 110 | uint32_t tmp; |
| 111 | |
| 112 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 113 | tmp &= ~(1 << FL_SCK); |
| 114 | tmp |= (val << FL_SCK); |
| 115 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 116 | } |
| 117 | |
| 118 | static void nicintel_bitbang_set_mosi(int val) |
| 119 | { |
| 120 | uint32_t tmp; |
| 121 | |
| 122 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 123 | tmp &= ~(1 << FL_SI); |
| 124 | tmp |= (val << FL_SI); |
| 125 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 126 | } |
| 127 | |
| 128 | static int nicintel_bitbang_get_miso(void) |
| 129 | { |
| 130 | uint32_t tmp; |
| 131 | |
| 132 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 133 | tmp = (tmp >> FL_SO) & 0x1; |
| 134 | return tmp; |
| 135 | } |
| 136 | |
| 137 | static const struct bitbang_spi_master bitbang_spi_master_nicintel = { |
| 138 | .type = BITBANG_SPI_MASTER_NICINTEL, |
| 139 | .set_cs = nicintel_bitbang_set_cs, |
| 140 | .set_sck = nicintel_bitbang_set_sck, |
| 141 | .set_mosi = nicintel_bitbang_set_mosi, |
| 142 | .get_miso = nicintel_bitbang_get_miso, |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 143 | .request_bus = nicintel_request_spibus, |
| 144 | .release_bus = nicintel_release_spibus, |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 145 | .half_period = 1, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 148 | static int nicintel_spi_shutdown(void *data) |
| 149 | { |
| 150 | uint32_t tmp; |
| 151 | |
| 152 | /* Disable writes manually. See the comment about EECD in |
| 153 | * nicintel_spi_init() for details. |
| 154 | */ |
| 155 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 156 | tmp &= ~FLASH_WRITES_ENABLED; |
| 157 | tmp |= FLASH_WRITES_DISABLED; |
| 158 | pci_mmio_writel(tmp, nicintel_spibar + EECD); |
| 159 | |
| 160 | physunmap(nicintel_spibar, 4096); |
| 161 | pci_cleanup(pacc); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 166 | int nicintel_spi_init(void) |
| 167 | { |
| 168 | uint32_t tmp; |
| 169 | |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 170 | if (rget_io_perms()) |
| 171 | return 1; |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 172 | |
Carl-Daniel Hailfinger | 40446ee | 2011-03-07 01:08:09 +0000 | [diff] [blame] | 173 | io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, nics_intel_spi); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 174 | |
| 175 | nicintel_spibar = physmap("Intel Gigabit NIC w/ SPI flash", |
| 176 | io_base_addr, 4096); |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 177 | /* Automatic restore of EECD on shutdown is not possible because EECD |
| 178 | * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED, |
| 179 | * but other bits with side effects as well. Those other bits must be |
| 180 | * left untouched. |
| 181 | */ |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 182 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 183 | tmp &= ~FLASH_WRITES_DISABLED; |
| 184 | tmp |= FLASH_WRITES_ENABLED; |
| 185 | pci_mmio_writel(tmp, nicintel_spibar + EECD); |
| 186 | |
Stefan Tauner | 8ee180d | 2012-02-27 19:44:16 +0000 | [diff] [blame] | 187 | /* test if FWE is really set to allow writes */ |
| 188 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 189 | if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) { |
| 190 | msg_perr("Enabling flash write access failed.\n"); |
| 191 | return 1; |
| 192 | } |
| 193 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 194 | if (register_shutdown(nicintel_spi_shutdown, NULL)) |
| 195 | return 1; |
| 196 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 197 | if (bitbang_spi_init(&bitbang_spi_master_nicintel)) |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 198 | return 1; |
| 199 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 200 | return 0; |
| 201 | } |