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