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