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" |
| 31 | |
| 32 | #define PCI_VENDOR_ID_INTEL 0x8086 |
| 33 | |
| 34 | #define EECD 0x10 |
| 35 | #define FLA 0x1c |
| 36 | |
| 37 | /* |
| 38 | * Register bits of EECD. |
| 39 | * |
| 40 | * Bit 04, 05: FWE (Flash Write Enable Control) |
| 41 | * 00b = not allowed |
| 42 | * 01b = flash writes disabled |
| 43 | * 10b = flash writes enabled |
| 44 | * 11b = not allowed |
| 45 | */ |
| 46 | #define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */ |
| 47 | #define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */ |
| 48 | |
| 49 | /* Flash Access register bits */ |
| 50 | /* Table 13-9 */ |
| 51 | #define FL_SCK 0 |
| 52 | #define FL_CS 1 |
| 53 | #define FL_SI 2 |
| 54 | #define FL_SO 3 |
| 55 | #define FL_REQ 4 |
| 56 | #define FL_GNT 5 |
| 57 | /* Currently unused */ |
| 58 | // #define FL_BUSY 30 |
| 59 | // #define FL_ER 31 |
| 60 | |
| 61 | uint8_t *nicintel_spibar; |
| 62 | |
| 63 | const struct pcidev_status nics_intel_spi[] = { |
| 64 | {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"}, |
| 65 | |
| 66 | {}, |
| 67 | }; |
| 68 | |
| 69 | static void nicintel_request_spibus(void) |
| 70 | { |
| 71 | uint32_t tmp; |
| 72 | |
| 73 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 74 | tmp |= 1 << FL_REQ; |
| 75 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 76 | |
| 77 | /* Wait until we are allowed to use the SPI bus. */ |
| 78 | while (!(pci_mmio_readl(nicintel_spibar + FLA) & (1 << FL_GNT))) ; |
| 79 | } |
| 80 | |
| 81 | static void nicintel_release_spibus(void) |
| 82 | { |
| 83 | uint32_t tmp; |
| 84 | |
| 85 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 86 | tmp &= ~(1 << FL_REQ); |
| 87 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 88 | } |
| 89 | |
| 90 | static void nicintel_bitbang_set_cs(int val) |
| 91 | { |
| 92 | uint32_t tmp; |
| 93 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 94 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 95 | tmp &= ~(1 << FL_CS); |
| 96 | tmp |= (val << FL_CS); |
| 97 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void nicintel_bitbang_set_sck(int val) |
| 101 | { |
| 102 | uint32_t tmp; |
| 103 | |
| 104 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 105 | tmp &= ~(1 << FL_SCK); |
| 106 | tmp |= (val << FL_SCK); |
| 107 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 108 | } |
| 109 | |
| 110 | static void nicintel_bitbang_set_mosi(int val) |
| 111 | { |
| 112 | uint32_t tmp; |
| 113 | |
| 114 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 115 | tmp &= ~(1 << FL_SI); |
| 116 | tmp |= (val << FL_SI); |
| 117 | pci_mmio_writel(tmp, nicintel_spibar + FLA); |
| 118 | } |
| 119 | |
| 120 | static int nicintel_bitbang_get_miso(void) |
| 121 | { |
| 122 | uint32_t tmp; |
| 123 | |
| 124 | tmp = pci_mmio_readl(nicintel_spibar + FLA); |
| 125 | tmp = (tmp >> FL_SO) & 0x1; |
| 126 | return tmp; |
| 127 | } |
| 128 | |
| 129 | static const struct bitbang_spi_master bitbang_spi_master_nicintel = { |
| 130 | .type = BITBANG_SPI_MASTER_NICINTEL, |
| 131 | .set_cs = nicintel_bitbang_set_cs, |
| 132 | .set_sck = nicintel_bitbang_set_sck, |
| 133 | .set_mosi = nicintel_bitbang_set_mosi, |
| 134 | .get_miso = nicintel_bitbang_get_miso, |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 135 | .request_bus = nicintel_request_spibus, |
| 136 | .release_bus = nicintel_release_spibus, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | int nicintel_spi_init(void) |
| 140 | { |
| 141 | uint32_t tmp; |
| 142 | |
| 143 | get_io_perms(); |
| 144 | |
| 145 | io_base_addr = pcidev_init(PCI_VENDOR_ID_INTEL, PCI_BASE_ADDRESS_0, |
| 146 | nics_intel_spi); |
| 147 | |
| 148 | nicintel_spibar = physmap("Intel Gigabit NIC w/ SPI flash", |
| 149 | io_base_addr, 4096); |
| 150 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 151 | tmp &= ~FLASH_WRITES_DISABLED; |
| 152 | tmp |= FLASH_WRITES_ENABLED; |
| 153 | pci_mmio_writel(tmp, nicintel_spibar + EECD); |
| 154 | |
| 155 | /* 1 usec halfperiod delay for now. */ |
| 156 | if (bitbang_spi_init(&bitbang_spi_master_nicintel, 1)) |
| 157 | return 1; |
| 158 | |
| 159 | buses_supported = CHIP_BUSTYPE_SPI; |
| 160 | spi_controller = SPI_CONTROLLER_NICINTEL; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | int nicintel_spi_shutdown(void) |
| 166 | { |
| 167 | uint32_t tmp; |
| 168 | |
| 169 | tmp = pci_mmio_readl(nicintel_spibar + EECD); |
| 170 | tmp &= ~FLASH_WRITES_ENABLED; |
| 171 | tmp |= FLASH_WRITES_DISABLED; |
| 172 | pci_mmio_writel(tmp, nicintel_spibar + EECD); |
| 173 | |
| 174 | physunmap(nicintel_spibar, 4096); |
| 175 | pci_cleanup(pacc); |
| 176 | release_io_perms(); |
| 177 | |
| 178 | return 0; |
| 179 | } |