zbao | 246e84b | 2012-07-13 18:47:03 +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 | 246e84b | 2012-07-13 18:47:03 +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 | 246e84b | 2012-07-13 18:47:03 +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 | 246e84b | 2012-07-13 18:47:03 +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 | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 27 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 28 | #if defined (CONFIG_HUDSON_IMC_FWM) |
Alexandru Gagniuc | 01e0adf | 2014-03-29 17:07:26 -0500 | [diff] [blame] | 29 | #include <Proc/Fch/FchPlatform.h> |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 30 | |
| 31 | static int bus_claimed = 0; |
| 32 | #endif |
| 33 | |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 34 | #define SPI_REG_OPCODE 0x0 |
| 35 | #define SPI_REG_CNTRL01 0x1 |
| 36 | #define SPI_REG_CNTRL02 0x2 |
| 37 | #define CNTRL02_FIFO_RESET (1 << 4) |
| 38 | #define CNTRL02_EXEC_OPCODE (1 << 0) |
| 39 | #define SPI_REG_CNTRL03 0x3 |
| 40 | #define CNTRL03_SPIBUSY (1 << 7) |
| 41 | #define SPI_REG_FIFO 0xc |
| 42 | #define SPI_REG_CNTRL11 0xd |
| 43 | #define CNTRL11_FIFOPTR_MASK 0x07 |
| 44 | |
| 45 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 46 | static u32 spibar; |
| 47 | |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 48 | static inline uint8_t spi_read(uint8_t reg) |
| 49 | { |
| 50 | return read8(spibar + reg); |
| 51 | } |
| 52 | |
| 53 | static inline void spi_write(uint8_t reg, uint8_t val) |
| 54 | { |
| 55 | write8(spibar + reg, val); |
| 56 | } |
| 57 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 58 | static void reset_internal_fifo_pointer(void) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 59 | { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 60 | uint8_t reg8; |
| 61 | |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 62 | do { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 63 | reg8 = spi_read(SPI_REG_CNTRL02); |
| 64 | reg8 |= CNTRL02_FIFO_RESET; |
| 65 | spi_write(SPI_REG_CNTRL02, reg8); |
| 66 | } while (spi_read(SPI_REG_CNTRL11) & CNTRL11_FIFOPTR_MASK); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 69 | static void execute_command(void) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 70 | { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 71 | uint8_t reg8; |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 72 | |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 73 | reg8 = spi_read(SPI_REG_CNTRL02); |
| 74 | reg8 |= CNTRL02_EXEC_OPCODE; |
| 75 | spi_write(SPI_REG_CNTRL02, reg8); |
| 76 | |
| 77 | while ((spi_read(SPI_REG_CNTRL02) & CNTRL02_EXEC_OPCODE) && |
| 78 | (spi_read(SPI_REG_CNTRL03) & CNTRL03_SPIBUSY)); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 79 | } |
| 80 | |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 81 | void spi_init(void) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 82 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 83 | device_t dev; |
| 84 | |
| 85 | dev = dev_find_slot(0, PCI_DEVFN(0x14, 3)); |
| 86 | spibar = pci_read_config32(dev, 0xA0) & ~0x1F; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 87 | } |
| 88 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 89 | int spi_xfer(struct spi_slave *slave, const void *dout, |
| 90 | unsigned int bitsout, void *din, unsigned int bitsin) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 91 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 92 | /* First byte is cmd which can not being sent through FIFO. */ |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 93 | uint8_t cmd = *(uint8_t *)dout++; |
| 94 | uint8_t readoffby1; |
Siyuan Wang | 9157145 | 2013-07-09 17:32:42 +0800 | [diff] [blame] | 95 | #if !CONFIG_SOUTHBRIDGE_AMD_AGESA_YANGTZE |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 96 | uint8_t readwrite; |
Siyuan Wang | 9157145 | 2013-07-09 17:32:42 +0800 | [diff] [blame] | 97 | #endif |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 98 | uint8_t bytesout, bytesin; |
| 99 | uint8_t count; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 100 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 101 | bitsout -= 8; |
| 102 | bytesout = bitsout / 8; |
| 103 | bytesin = bitsin / 8; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 104 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 105 | readoffby1 = bytesout ? 0 : 1; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 106 | |
Siyuan Wang | 9157145 | 2013-07-09 17:32:42 +0800 | [diff] [blame] | 107 | #if CONFIG_SOUTHBRIDGE_AMD_AGESA_YANGTZE |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 108 | spi_write(0x1E, 5); |
| 109 | spi_write(0x1F, bytesout); /* SpiExtRegIndx [5] - TxByteCount */ |
| 110 | spi_write(0x1E, 6); |
| 111 | spi_write(0x1F, bytesin); /* SpiExtRegIndx [6] - RxByteCount */ |
Siyuan Wang | 9157145 | 2013-07-09 17:32:42 +0800 | [diff] [blame] | 112 | #else |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 113 | readwrite = (bytesin + readoffby1) << 4 | bytesout; |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 114 | spi_write(SPI_REG_CNTRL01, readwrite); |
Siyuan Wang | 9157145 | 2013-07-09 17:32:42 +0800 | [diff] [blame] | 115 | #endif |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 116 | spi_write(SPI_REG_OPCODE, cmd); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 117 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 118 | reset_internal_fifo_pointer(); |
| 119 | for (count = 0; count < bytesout; count++, dout++) { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 120 | spi_write(SPI_REG_FIFO, *(uint8_t *)dout); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 121 | } |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 122 | |
| 123 | reset_internal_fifo_pointer(); |
| 124 | execute_command(); |
| 125 | |
| 126 | reset_internal_fifo_pointer(); |
| 127 | /* Skip the bytes we sent. */ |
| 128 | for (count = 0; count < bytesout; count++) { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 129 | cmd = spi_read(SPI_REG_FIFO); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | reset_internal_fifo_pointer(); |
| 133 | for (count = 0; count < bytesin; count++, din++) { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame^] | 134 | *(uint8_t *)din = spi_read(SPI_REG_FIFO); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | int spi_claim_bus(struct spi_slave *slave) |
| 140 | { |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 141 | #if defined (CONFIG_HUDSON_IMC_FWM) |
| 142 | |
| 143 | if (slave->rw == SPI_WRITE_FLAG) { |
| 144 | bus_claimed++; |
| 145 | if (bus_claimed == 1) |
| 146 | ImcSleep(NULL); |
| 147 | } |
| 148 | #endif |
| 149 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 150 | return 0; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 151 | } |
| 152 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 153 | void spi_release_bus(struct spi_slave *slave) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 154 | { |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 155 | #if defined (CONFIG_HUDSON_IMC_FWM) |
| 156 | |
| 157 | if (slave->rw == SPI_WRITE_FLAG) { |
| 158 | bus_claimed--; |
| 159 | if (bus_claimed <= 0) { |
| 160 | bus_claimed = 0; |
| 161 | ImcWakeup(NULL); |
| 162 | } |
| 163 | } |
| 164 | #endif |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 167 | void spi_cs_activate(struct spi_slave *slave) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 168 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void spi_cs_deactivate(struct spi_slave *slave) |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 176 | unsigned int max_hz, unsigned int mode) |
| 177 | { |
| 178 | struct spi_slave *slave = malloc(sizeof(*slave)); |
| 179 | |
| 180 | if (!slave) { |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | memset(slave, 0, sizeof(*slave)); |
| 185 | |
| 186 | return slave; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 187 | } |