Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 20 | /* Driver for the NVIDIA MCP6x/MCP7x MCP6X_SPI controller. |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 21 | * Based on clean room reverse engineered docs from |
| 22 | * http://www.flashrom.org/pipermail/flashrom/2009-December/001180.html |
| 23 | * created by Michael Karcher. |
| 24 | */ |
| 25 | |
| 26 | #if defined(__i386__) || defined(__x86_64__) |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <ctype.h> |
| 31 | #include "flash.h" |
| 32 | #include "programmer.h" |
| 33 | |
| 34 | /* Bit positions for each pin. */ |
| 35 | |
| 36 | #define MCP6X_SPI_CS 1 |
| 37 | #define MCP6X_SPI_SCK 2 |
| 38 | #define MCP6X_SPI_MOSI 3 |
| 39 | #define MCP6X_SPI_MISO 4 |
| 40 | #define MCP6X_SPI_REQUEST 0 |
| 41 | #define MCP6X_SPI_GRANT 8 |
| 42 | |
| 43 | void *mcp6x_spibar = NULL; |
| 44 | |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 45 | /* Cached value of last GPIO state. */ |
| 46 | static uint8_t mcp_gpiostate; |
| 47 | |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 48 | static void mcp6x_request_spibus(void) |
| 49 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 50 | mcp_gpiostate = mmio_readb(mcp6x_spibar + 0x530); |
| 51 | mcp_gpiostate |= 1 << MCP6X_SPI_REQUEST; |
| 52 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 53 | |
| 54 | /* Wait until we are allowed to use the SPI bus. */ |
| 55 | while (!(mmio_readw(mcp6x_spibar + 0x530) & (1 << MCP6X_SPI_GRANT))) ; |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 56 | |
| 57 | /* Update the cache. */ |
| 58 | mcp_gpiostate = mmio_readb(mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static void mcp6x_release_spibus(void) |
| 62 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 63 | mcp_gpiostate &= ~(1 << MCP6X_SPI_REQUEST); |
| 64 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void mcp6x_bitbang_set_cs(int val) |
| 68 | { |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 69 | /* Requesting and releasing the SPI bus is handled in here to allow the |
| 70 | * chipset to use its own SPI engine for native reads. |
| 71 | */ |
| 72 | if (val == 0) |
| 73 | mcp6x_request_spibus(); |
| 74 | |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 75 | mcp_gpiostate &= ~(1 << MCP6X_SPI_CS); |
| 76 | mcp_gpiostate |= (val << MCP6X_SPI_CS); |
| 77 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 78 | |
| 79 | if (val == 1) |
| 80 | mcp6x_release_spibus(); |
| 81 | } |
| 82 | |
| 83 | static void mcp6x_bitbang_set_sck(int val) |
| 84 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 85 | mcp_gpiostate &= ~(1 << MCP6X_SPI_SCK); |
| 86 | mcp_gpiostate |= (val << MCP6X_SPI_SCK); |
| 87 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void mcp6x_bitbang_set_mosi(int val) |
| 91 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 92 | mcp_gpiostate &= ~(1 << MCP6X_SPI_MOSI); |
| 93 | mcp_gpiostate |= (val << MCP6X_SPI_MOSI); |
| 94 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int mcp6x_bitbang_get_miso(void) |
| 98 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 99 | mcp_gpiostate = mmio_readb(mcp6x_spibar + 0x530); |
| 100 | return (mcp_gpiostate >> MCP6X_SPI_MISO) & 0x1; |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static const struct bitbang_spi_master bitbang_spi_master_mcp6x = { |
| 104 | .type = BITBANG_SPI_MASTER_MCP, |
| 105 | .set_cs = mcp6x_bitbang_set_cs, |
| 106 | .set_sck = mcp6x_bitbang_set_sck, |
| 107 | .set_mosi = mcp6x_bitbang_set_mosi, |
| 108 | .get_miso = mcp6x_bitbang_get_miso, |
| 109 | }; |
| 110 | |
| 111 | int mcp6x_spi_init(int want_spi) |
| 112 | { |
| 113 | uint16_t status; |
| 114 | uint32_t mcp6x_spibaraddr; |
| 115 | struct pci_dev *smbusdev; |
| 116 | |
| 117 | /* Look for the SMBus device (SMBus PCI class) */ |
| 118 | smbusdev = pci_dev_find_vendorclass(0x10de, 0x0c05); |
| 119 | if (!smbusdev) { |
| 120 | if (want_spi) { |
| 121 | msg_perr("ERROR: SMBus device not found. Not enabling " |
| 122 | "SPI.\n"); |
| 123 | return 1; |
| 124 | } else { |
| 125 | msg_pinfo("Odd. SMBus device not found.\n"); |
| 126 | return 0; |
| 127 | } |
| 128 | } |
| 129 | msg_pdbg("Found SMBus device %04x:%04x at %02x:%02x:%01x\n", |
| 130 | smbusdev->vendor_id, smbusdev->device_id, |
| 131 | smbusdev->bus, smbusdev->dev, smbusdev->func); |
| 132 | |
| 133 | |
| 134 | /* Locate the BAR where the SPI interface lives. */ |
| 135 | mcp6x_spibaraddr = pci_read_long(smbusdev, 0x74); |
| 136 | /* BAR size is 64k, bits 15..4 are zero, bit 3..0 declare a |
| 137 | * 32-bit non-prefetchable memory BAR. |
| 138 | */ |
| 139 | mcp6x_spibaraddr &= ~0xffff; |
| 140 | msg_pdbg("MCP SPI BAR is at 0x%08x\n", mcp6x_spibaraddr); |
| 141 | |
| 142 | /* Accessing a NULL pointer BAR is evil. Don't do it. */ |
| 143 | if (!mcp6x_spibaraddr && want_spi) { |
| 144 | msg_perr("Error: Chipset is strapped for SPI, but MCP SPI BAR " |
| 145 | "is invalid.\n"); |
| 146 | return 1; |
| 147 | } else if (!mcp6x_spibaraddr && !want_spi) { |
| 148 | msg_pdbg("MCP SPI is not used.\n"); |
| 149 | return 0; |
| 150 | } else if (mcp6x_spibaraddr && !want_spi) { |
| 151 | msg_pdbg("Strange. MCP SPI BAR is valid, but chipset apparently" |
| 152 | " doesn't have SPI enabled.\n"); |
| 153 | /* FIXME: Should we enable SPI anyway? */ |
| 154 | return 0; |
| 155 | } |
| 156 | /* Map the BAR. Bytewise/wordwise access at 0x530 and 0x540. */ |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 157 | mcp6x_spibar = physmap("NVIDIA MCP6x SPI", mcp6x_spibaraddr, 0x544); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 158 | |
| 159 | #if 0 |
| 160 | /* FIXME: Run the physunmap in a shutdown function. */ |
| 161 | physunmap(mcp6x_spibar, 0x544); |
| 162 | #endif |
| 163 | |
| 164 | status = mmio_readw(mcp6x_spibar + 0x530); |
| 165 | msg_pdbg("SPI control is 0x%04x, req=%i, gnt=%i\n", |
| 166 | status, (status >> MCP6X_SPI_REQUEST) & 0x1, |
| 167 | (status >> MCP6X_SPI_GRANT) & 0x1); |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 168 | mcp_gpiostate = status & 0xff; |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 169 | |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 170 | /* Zero halfperiod delay. */ |
| 171 | if (bitbang_spi_init(&bitbang_spi_master_mcp6x, 0)) { |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 172 | /* This should never happen. */ |
| 173 | msg_perr("MCP6X bitbang SPI master init failed!\n"); |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | buses_supported |= CHIP_BUSTYPE_SPI; |
| 178 | spi_controller = SPI_CONTROLLER_MCP6X_BITBANG; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | #endif |