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 | |
Dave Frodin | 9b800ae | 2014-06-11 13:15:56 -0600 | [diff] [blame] | 28 | #if IS_ENABLED (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, |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame^] | 90 | unsigned int bytesout, void *din, unsigned int bytesin) |
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. */ |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame^] | 93 | u8 cmd = *(u8 *)dout++; |
| 94 | u8 readoffby1; |
| 95 | u8 count; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 96 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame^] | 97 | bytesout--; |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 98 | readoffby1 = bytesout ? 0 : 1; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 99 | |
Siyuan Wang | 9157145 | 2013-07-09 17:32:42 +0800 | [diff] [blame] | 100 | #if CONFIG_SOUTHBRIDGE_AMD_AGESA_YANGTZE |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame] | 101 | spi_write(0x1E, 5); |
| 102 | spi_write(0x1F, bytesout); /* SpiExtRegIndx [5] - TxByteCount */ |
| 103 | spi_write(0x1E, 6); |
| 104 | spi_write(0x1F, bytesin); /* SpiExtRegIndx [6] - RxByteCount */ |
Siyuan Wang | 9157145 | 2013-07-09 17:32:42 +0800 | [diff] [blame] | 105 | #else |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame^] | 106 | u8 readwrite = (bytesin + readoffby1) << 4 | bytesout; |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame] | 107 | spi_write(SPI_REG_CNTRL01, readwrite); |
Siyuan Wang | 9157145 | 2013-07-09 17:32:42 +0800 | [diff] [blame] | 108 | #endif |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame] | 109 | spi_write(SPI_REG_OPCODE, cmd); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 110 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 111 | reset_internal_fifo_pointer(); |
| 112 | for (count = 0; count < bytesout; count++, dout++) { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame] | 113 | spi_write(SPI_REG_FIFO, *(uint8_t *)dout); |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 114 | } |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 115 | |
| 116 | reset_internal_fifo_pointer(); |
| 117 | execute_command(); |
| 118 | |
| 119 | reset_internal_fifo_pointer(); |
| 120 | /* Skip the bytes we sent. */ |
| 121 | for (count = 0; count < bytesout; count++) { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame] | 122 | cmd = spi_read(SPI_REG_FIFO); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | reset_internal_fifo_pointer(); |
| 126 | for (count = 0; count < bytesin; count++, din++) { |
Alexandru Gagniuc | 991e951 | 2014-04-19 22:21:27 -0500 | [diff] [blame] | 127 | *(uint8_t *)din = spi_read(SPI_REG_FIFO); |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | int spi_claim_bus(struct spi_slave *slave) |
| 133 | { |
Dave Frodin | 9b800ae | 2014-06-11 13:15:56 -0600 | [diff] [blame] | 134 | #if IS_ENABLED (CONFIG_HUDSON_IMC_FWM) |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 135 | |
| 136 | if (slave->rw == SPI_WRITE_FLAG) { |
| 137 | bus_claimed++; |
| 138 | if (bus_claimed == 1) |
| 139 | ImcSleep(NULL); |
| 140 | } |
| 141 | #endif |
| 142 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 143 | return 0; |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 144 | } |
| 145 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 146 | void spi_release_bus(struct spi_slave *slave) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 147 | { |
Dave Frodin | 9b800ae | 2014-06-11 13:15:56 -0600 | [diff] [blame] | 148 | #if IS_ENABLED (CONFIG_HUDSON_IMC_FWM) |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 149 | |
| 150 | if (slave->rw == SPI_WRITE_FLAG) { |
| 151 | bus_claimed--; |
| 152 | if (bus_claimed <= 0) { |
| 153 | bus_claimed = 0; |
| 154 | ImcWakeup(NULL); |
| 155 | } |
| 156 | } |
| 157 | #endif |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 158 | } |
| 159 | |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 160 | void spi_cs_activate(struct spi_slave *slave) |
zbao | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 161 | { |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void spi_cs_deactivate(struct spi_slave *slave) |
| 165 | { |
| 166 | } |
| 167 | |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 168 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs) |
Zheng Bao | 7bcffa5 | 2012-11-28 11:36:52 +0800 | [diff] [blame] | 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 | 246e84b | 2012-07-13 18:47:03 +0800 | [diff] [blame] | 179 | } |