zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Advanced Micro Devices, Inc. |
| 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 |
Paul Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 18 | */ |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 22 | #include <arch/io.h> |
Kyösti Mälkki | 9f0a2be | 2014-06-30 07:34:36 +0300 | [diff] [blame] | 23 | #include <console/console.h> |
Zheng Bao | 600784e | 2013-02-07 17:30:23 +0800 | [diff] [blame] | 24 | #include <spi-generic.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 25 | #include <device/device.h> |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 26 | #include <device/pci.h> |
| 27 | #include <device/pci_ops.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 28 | |
Dave Frodin | 9b800ae | 2014-06-11 13:15:56 -0600 | [diff] [blame] | 29 | #if IS_ENABLED (CONFIG_SB800_IMC_FWM) |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 30 | #include "SBPLATFORM.h" |
| 31 | #include <vendorcode/amd/cimx/sb800/ECfan.h> |
| 32 | |
| 33 | static int bus_claimed = 0; |
| 34 | #endif |
| 35 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 36 | #define AMD_SB_SPI_TX_LEN 8 |
| 37 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 38 | static u32 spibar; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 39 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 40 | static void reset_internal_fifo_pointer(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 41 | { |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 42 | do { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame^] | 43 | write8((void *)(spibar + 2), |
| 44 | read8((void *)(spibar + 2)) | 0x10); |
| 45 | } while (read8((void *)(spibar + 0xD)) & 0x7); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 46 | } |
| 47 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 48 | static void execute_command(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 49 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame^] | 50 | write8((void *)(spibar + 2), read8((void *)(spibar + 2)) | 1); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 51 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame^] | 52 | while ((read8((void *)(spibar + 2)) & 1) && |
| 53 | (read8((void *)(spibar+3)) & 0x80)); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 56 | void spi_init() |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 57 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 58 | device_t dev; |
| 59 | |
| 60 | dev = dev_find_slot(0, PCI_DEVFN(0x14, 3)); |
| 61 | spibar = pci_read_config32(dev, 0xA0) & ~0x1F; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 62 | } |
| 63 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 64 | unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len) |
| 65 | { |
| 66 | return min(AMD_SB_SPI_TX_LEN - cmd_len, buf_len); |
| 67 | } |
| 68 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 69 | int spi_xfer(struct spi_slave *slave, const void *dout, |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 70 | unsigned int bytesout, void *din, unsigned int bytesin) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 71 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 72 | /* First byte is cmd which can not being sent through FIFO. */ |
| 73 | u8 cmd = *(u8 *)dout++; |
| 74 | u8 readoffby1; |
| 75 | u8 readwrite; |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 76 | u8 count; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 77 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 78 | bytesout--; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 79 | |
Kyösti Mälkki | 9f0a2be | 2014-06-30 07:34:36 +0300 | [diff] [blame] | 80 | /* |
| 81 | * Check if this is a write command attempting to transfer more bytes |
| 82 | * than the controller can handle. Iterations for writes are not |
| 83 | * supported here because each SPI write command needs to be preceded |
| 84 | * and followed by other SPI commands, and this sequence is controlled |
| 85 | * by the SPI chip driver. |
| 86 | */ |
| 87 | if (bytesout > AMD_SB_SPI_TX_LEN) { |
| 88 | printk(BIOS_DEBUG, "FCH SPI: Too much to write. Does your SPI chip driver use" |
| 89 | " spi_crop_chunk()?\n"); |
| 90 | return -1; |
| 91 | } |
| 92 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 93 | readoffby1 = bytesout ? 0 : 1; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 94 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 95 | readwrite = (bytesin + readoffby1) << 4 | bytesout; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame^] | 96 | write8((void *)(spibar + 1), readwrite); |
| 97 | write8((void *)(spibar + 0), cmd); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 98 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 99 | reset_internal_fifo_pointer(); |
| 100 | for (count = 0; count < bytesout; count++, dout++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame^] | 101 | write8((void *)(spibar + 0x0C), *(u8 *)dout); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 102 | } |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 103 | |
| 104 | reset_internal_fifo_pointer(); |
| 105 | execute_command(); |
| 106 | |
| 107 | reset_internal_fifo_pointer(); |
| 108 | /* Skip the bytes we sent. */ |
| 109 | for (count = 0; count < bytesout; count++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame^] | 110 | cmd = read8((void *)(spibar + 0x0C)); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | reset_internal_fifo_pointer(); |
| 114 | for (count = 0; count < bytesin; count++, din++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame^] | 115 | *(u8 *)din = read8((void *)(spibar + 0x0C)); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 120 | |
Dave Frodin | 9b800ae | 2014-06-11 13:15:56 -0600 | [diff] [blame] | 121 | #if IS_ENABLED (CONFIG_SB800_IMC_FWM) |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 122 | |
| 123 | static void ImcSleep(void) |
| 124 | { |
| 125 | u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */ |
| 126 | u8 reg0_val = 0; /* clear response register */ |
| 127 | u8 reg1_val = 0xB4; /* request ownership flag */ |
| 128 | |
| 129 | WriteECmsg (MSG_REG0, AccWidthUint8, ®0_val); |
| 130 | WriteECmsg (MSG_REG1, AccWidthUint8, ®1_val); |
| 131 | WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val); |
| 132 | |
| 133 | WaitForEcLDN9MailboxCmdAck(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | static void ImcWakeup(void) |
| 138 | { |
| 139 | u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */ |
Idwer Vollering | d26da9c | 2013-12-22 21:38:18 +0000 | [diff] [blame] | 140 | u8 reg0_val = 0; /* clear response register */ |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 141 | u8 reg1_val = 0xB5; /* release ownership flag */ |
| 142 | |
| 143 | WriteECmsg (MSG_REG0, AccWidthUint8, ®0_val); |
| 144 | WriteECmsg (MSG_REG1, AccWidthUint8, ®1_val); |
| 145 | WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val); |
| 146 | |
| 147 | WaitForEcLDN9MailboxCmdAck(); |
| 148 | } |
| 149 | #endif |
| 150 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 151 | int spi_claim_bus(struct spi_slave *slave) |
| 152 | { |
Dave Frodin | 9b800ae | 2014-06-11 13:15:56 -0600 | [diff] [blame] | 153 | #if IS_ENABLED (CONFIG_SB800_IMC_FWM) |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 154 | |
| 155 | if (slave->rw == SPI_WRITE_FLAG) { |
| 156 | bus_claimed++; |
| 157 | if (bus_claimed == 1) |
| 158 | ImcSleep(); |
| 159 | } |
| 160 | #endif |
| 161 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 162 | return 0; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 165 | void spi_release_bus(struct spi_slave *slave) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 166 | { |
Dave Frodin | 9b800ae | 2014-06-11 13:15:56 -0600 | [diff] [blame] | 167 | #if IS_ENABLED (CONFIG_SB800_IMC_FWM) |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 168 | |
| 169 | if (slave->rw == SPI_WRITE_FLAG) { |
| 170 | bus_claimed--; |
| 171 | if (bus_claimed <= 0) { |
| 172 | bus_claimed = 0; |
| 173 | ImcWakeup(); |
| 174 | } |
| 175 | } |
| 176 | #endif |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 179 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs) |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 180 | { |
| 181 | struct spi_slave *slave = malloc(sizeof(*slave)); |
| 182 | |
| 183 | if (!slave) { |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | memset(slave, 0, sizeof(*slave)); |
| 188 | |
| 189 | return slave; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 190 | } |