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. |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 16 | /* Driver for the NVIDIA MCP6x/MCP7x MCP6X_SPI controller. |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 17 | * Based on clean room reverse engineered docs from |
Stefan Tauner | 4c72315 | 2016-01-14 22:47:55 +0000 | [diff] [blame] | 18 | * https://flashrom.org/pipermail/flashrom/2009-December/001180.html |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 19 | * created by Michael Karcher. |
| 20 | */ |
| 21 | |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <ctype.h> |
| 24 | #include "flash.h" |
| 25 | #include "programmer.h" |
Thomas Heijligen | 74b4aa0 | 2021-12-14 17:52:30 +0100 | [diff] [blame] | 26 | #include "hwaccess_physmap.h" |
Thomas Heijligen | d96c97c | 2021-11-02 21:03:00 +0100 | [diff] [blame] | 27 | #include "platform/pci.h" |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 28 | |
| 29 | /* Bit positions for each pin. */ |
| 30 | |
| 31 | #define MCP6X_SPI_CS 1 |
| 32 | #define MCP6X_SPI_SCK 2 |
| 33 | #define MCP6X_SPI_MOSI 3 |
| 34 | #define MCP6X_SPI_MISO 4 |
| 35 | #define MCP6X_SPI_REQUEST 0 |
| 36 | #define MCP6X_SPI_GRANT 8 |
| 37 | |
Jacob Garber | afc3ad6 | 2019-06-24 16:05:28 -0600 | [diff] [blame] | 38 | static void *mcp6x_spibar = NULL; |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 39 | |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 40 | /* Cached value of last GPIO state. */ |
| 41 | static uint8_t mcp_gpiostate; |
| 42 | |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 43 | static void mcp6x_request_spibus(void) |
| 44 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 45 | mcp_gpiostate = mmio_readb(mcp6x_spibar + 0x530); |
| 46 | mcp_gpiostate |= 1 << MCP6X_SPI_REQUEST; |
| 47 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 48 | |
| 49 | /* Wait until we are allowed to use the SPI bus. */ |
| 50 | while (!(mmio_readw(mcp6x_spibar + 0x530) & (1 << MCP6X_SPI_GRANT))) ; |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 51 | |
| 52 | /* Update the cache. */ |
| 53 | mcp_gpiostate = mmio_readb(mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void mcp6x_release_spibus(void) |
| 57 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 58 | mcp_gpiostate &= ~(1 << MCP6X_SPI_REQUEST); |
| 59 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static void mcp6x_bitbang_set_cs(int val) |
| 63 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 64 | mcp_gpiostate &= ~(1 << MCP6X_SPI_CS); |
| 65 | mcp_gpiostate |= (val << MCP6X_SPI_CS); |
| 66 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void mcp6x_bitbang_set_sck(int val) |
| 70 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 71 | mcp_gpiostate &= ~(1 << MCP6X_SPI_SCK); |
| 72 | mcp_gpiostate |= (val << MCP6X_SPI_SCK); |
| 73 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void mcp6x_bitbang_set_mosi(int val) |
| 77 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 78 | mcp_gpiostate &= ~(1 << MCP6X_SPI_MOSI); |
| 79 | mcp_gpiostate |= (val << MCP6X_SPI_MOSI); |
| 80 | mmio_writeb(mcp_gpiostate, mcp6x_spibar + 0x530); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static int mcp6x_bitbang_get_miso(void) |
| 84 | { |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 85 | mcp_gpiostate = mmio_readb(mcp6x_spibar + 0x530); |
| 86 | return (mcp_gpiostate >> MCP6X_SPI_MISO) & 0x1; |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static const struct bitbang_spi_master bitbang_spi_master_mcp6x = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 90 | .set_cs = mcp6x_bitbang_set_cs, |
| 91 | .set_sck = mcp6x_bitbang_set_sck, |
| 92 | .set_mosi = mcp6x_bitbang_set_mosi, |
| 93 | .get_miso = mcp6x_bitbang_get_miso, |
| 94 | .request_bus = mcp6x_request_spibus, |
| 95 | .release_bus = mcp6x_release_spibus, |
| 96 | .half_period = 0, |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | int mcp6x_spi_init(int want_spi) |
| 100 | { |
| 101 | uint16_t status; |
| 102 | uint32_t mcp6x_spibaraddr; |
| 103 | struct pci_dev *smbusdev; |
| 104 | |
| 105 | /* Look for the SMBus device (SMBus PCI class) */ |
| 106 | smbusdev = pci_dev_find_vendorclass(0x10de, 0x0c05); |
| 107 | if (!smbusdev) { |
| 108 | if (want_spi) { |
| 109 | msg_perr("ERROR: SMBus device not found. Not enabling " |
| 110 | "SPI.\n"); |
| 111 | return 1; |
| 112 | } else { |
| 113 | msg_pinfo("Odd. SMBus device not found.\n"); |
| 114 | return 0; |
| 115 | } |
| 116 | } |
| 117 | msg_pdbg("Found SMBus device %04x:%04x at %02x:%02x:%01x\n", |
| 118 | smbusdev->vendor_id, smbusdev->device_id, |
| 119 | smbusdev->bus, smbusdev->dev, smbusdev->func); |
| 120 | |
| 121 | |
| 122 | /* Locate the BAR where the SPI interface lives. */ |
| 123 | mcp6x_spibaraddr = pci_read_long(smbusdev, 0x74); |
| 124 | /* BAR size is 64k, bits 15..4 are zero, bit 3..0 declare a |
| 125 | * 32-bit non-prefetchable memory BAR. |
| 126 | */ |
| 127 | mcp6x_spibaraddr &= ~0xffff; |
| 128 | msg_pdbg("MCP SPI BAR is at 0x%08x\n", mcp6x_spibaraddr); |
| 129 | |
| 130 | /* Accessing a NULL pointer BAR is evil. Don't do it. */ |
| 131 | if (!mcp6x_spibaraddr && want_spi) { |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 132 | msg_perr("Error: Chipset is strapped for SPI, but MCP SPI BAR is invalid.\n"); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 133 | return 1; |
| 134 | } else if (!mcp6x_spibaraddr && !want_spi) { |
| 135 | msg_pdbg("MCP SPI is not used.\n"); |
| 136 | return 0; |
| 137 | } else if (mcp6x_spibaraddr && !want_spi) { |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 138 | msg_pdbg("Strange. MCP SPI BAR is valid, but chipset apparently doesn't have SPI enabled.\n"); |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 139 | /* FIXME: Should we enable SPI anyway? */ |
| 140 | return 0; |
| 141 | } |
| 142 | /* Map the BAR. Bytewise/wordwise access at 0x530 and 0x540. */ |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 143 | mcp6x_spibar = rphysmap("NVIDIA MCP6x SPI", mcp6x_spibaraddr, 0x544); |
| 144 | if (mcp6x_spibar == ERROR_PTR) |
| 145 | return 1; |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 146 | |
| 147 | status = mmio_readw(mcp6x_spibar + 0x530); |
| 148 | msg_pdbg("SPI control is 0x%04x, req=%i, gnt=%i\n", |
| 149 | status, (status >> MCP6X_SPI_REQUEST) & 0x1, |
| 150 | (status >> MCP6X_SPI_GRANT) & 0x1); |
Carl-Daniel Hailfinger | 7b61df8 | 2010-09-14 01:29:49 +0000 | [diff] [blame] | 151 | mcp_gpiostate = status & 0xff; |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 152 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 153 | if (register_spi_bitbang_master(&bitbang_spi_master_mcp6x)) { |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 154 | /* This should never happen. */ |
| 155 | msg_perr("MCP6X bitbang SPI master init failed!\n"); |
| 156 | return 1; |
| 157 | } |
| 158 | |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | } |