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> |
Zheng Bao | 600784e | 2013-02-07 17:30:23 +0800 | [diff] [blame] | 23 | #include <spi-generic.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 24 | #include <device/device.h> |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 25 | #include <device/pci.h> |
| 26 | #include <device/pci_ops.h> |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 27 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 28 | #if defined (CONFIG_SB800_IMC_FWM) |
| 29 | #include "SBPLATFORM.h" |
| 30 | #include <vendorcode/amd/cimx/sb800/ECfan.h> |
| 31 | |
| 32 | static int bus_claimed = 0; |
| 33 | #endif |
| 34 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 35 | static u32 spibar; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 36 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 37 | static void reset_internal_fifo_pointer(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 38 | { |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 39 | do { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 40 | write8(spibar + 2, read8(spibar + 2) | 0x10); |
| 41 | } while (read8(spibar + 0xD) & 0x7); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 42 | } |
| 43 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 44 | static void execute_command(void) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 45 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 46 | write8(spibar + 2, read8(spibar + 2) | 1); |
| 47 | |
| 48 | while ((read8(spibar + 2) & 1) && (read8(spibar+3) & 0x80)); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 51 | void spi_init() |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 52 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 53 | device_t dev; |
| 54 | |
| 55 | dev = dev_find_slot(0, PCI_DEVFN(0x14, 3)); |
| 56 | spibar = pci_read_config32(dev, 0xA0) & ~0x1F; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 57 | } |
| 58 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 59 | int spi_xfer(struct spi_slave *slave, const void *dout, |
| 60 | unsigned int bitsout, void *din, unsigned int bitsin) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 61 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 62 | /* First byte is cmd which can not being sent through FIFO. */ |
| 63 | u8 cmd = *(u8 *)dout++; |
| 64 | u8 readoffby1; |
| 65 | u8 readwrite; |
| 66 | u8 bytesout, bytesin; |
| 67 | u8 count; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 68 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 69 | bitsout -= 8; |
| 70 | bytesout = bitsout / 8; |
| 71 | bytesin = bitsin / 8; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 72 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 73 | readoffby1 = bytesout ? 0 : 1; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 74 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 75 | readwrite = (bytesin + readoffby1) << 4 | bytesout; |
| 76 | write8(spibar + 1, readwrite); |
| 77 | write8(spibar + 0, cmd); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 78 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 79 | reset_internal_fifo_pointer(); |
| 80 | for (count = 0; count < bytesout; count++, dout++) { |
| 81 | write8(spibar + 0x0C, *(u8 *)dout); |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 82 | } |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 83 | |
| 84 | reset_internal_fifo_pointer(); |
| 85 | execute_command(); |
| 86 | |
| 87 | reset_internal_fifo_pointer(); |
| 88 | /* Skip the bytes we sent. */ |
| 89 | for (count = 0; count < bytesout; count++) { |
| 90 | cmd = read8(spibar + 0x0C); |
| 91 | } |
| 92 | |
| 93 | reset_internal_fifo_pointer(); |
| 94 | for (count = 0; count < bytesin; count++, din++) { |
| 95 | *(u8 *)din = read8(spibar + 0x0C); |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 100 | |
| 101 | #if defined (CONFIG_SB800_IMC_FWM) |
| 102 | |
| 103 | static void ImcSleep(void) |
| 104 | { |
| 105 | u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */ |
| 106 | u8 reg0_val = 0; /* clear response register */ |
| 107 | u8 reg1_val = 0xB4; /* request ownership flag */ |
| 108 | |
| 109 | WriteECmsg (MSG_REG0, AccWidthUint8, ®0_val); |
| 110 | WriteECmsg (MSG_REG1, AccWidthUint8, ®1_val); |
| 111 | WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val); |
| 112 | |
| 113 | WaitForEcLDN9MailboxCmdAck(); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static void ImcWakeup(void) |
| 118 | { |
| 119 | u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */ |
Idwer Vollering | d26da9c | 2013-12-22 21:38:18 +0000 | [diff] [blame^] | 120 | u8 reg0_val = 0; /* clear response register */ |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 121 | u8 reg1_val = 0xB5; /* release ownership flag */ |
| 122 | |
| 123 | WriteECmsg (MSG_REG0, AccWidthUint8, ®0_val); |
| 124 | WriteECmsg (MSG_REG1, AccWidthUint8, ®1_val); |
| 125 | WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val); |
| 126 | |
| 127 | WaitForEcLDN9MailboxCmdAck(); |
| 128 | } |
| 129 | #endif |
| 130 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 131 | int spi_claim_bus(struct spi_slave *slave) |
| 132 | { |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 133 | #if defined (CONFIG_SB800_IMC_FWM) |
| 134 | |
| 135 | if (slave->rw == SPI_WRITE_FLAG) { |
| 136 | bus_claimed++; |
| 137 | if (bus_claimed == 1) |
| 138 | ImcSleep(); |
| 139 | } |
| 140 | #endif |
| 141 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 142 | return 0; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 143 | } |
| 144 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 145 | void spi_release_bus(struct spi_slave *slave) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 146 | { |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 147 | #if defined (CONFIG_SB800_IMC_FWM) |
| 148 | |
| 149 | if (slave->rw == SPI_WRITE_FLAG) { |
| 150 | bus_claimed--; |
| 151 | if (bus_claimed <= 0) { |
| 152 | bus_claimed = 0; |
| 153 | ImcWakeup(); |
| 154 | } |
| 155 | } |
| 156 | #endif |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 159 | void spi_cs_activate(struct spi_slave *slave) |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 160 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void spi_cs_deactivate(struct spi_slave *slave) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 168 | unsigned int max_hz, unsigned int mode) |
| 169 | { |
| 170 | struct spi_slave *slave = malloc(sizeof(*slave)); |
| 171 | |
| 172 | if (!slave) { |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | memset(slave, 0, sizeof(*slave)); |
| 177 | |
| 178 | return slave; |
zbao | 01bd79f | 2012-03-23 11:36:08 +0800 | [diff] [blame] | 179 | } |