blob: 013bb21a08be41556a8356b8cd2c4f60eb9bad96 [file] [log] [blame]
zbao246e84b2012-07-13 18:47:03 +08001/*
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 Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
zbao246e84b2012-07-13 18:47:03 +080018 */
Zheng Bao7bcffa52012-11-28 11:36:52 +080019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
zbao246e84b2012-07-13 18:47:03 +080022#include <arch/io.h>
Zheng Bao600784e2013-02-07 17:30:23 +080023#include <spi-generic.h>
zbao246e84b2012-07-13 18:47:03 +080024#include <device/device.h>
Zheng Bao7bcffa52012-11-28 11:36:52 +080025#include <device/pci.h>
26#include <device/pci_ops.h>
zbao246e84b2012-07-13 18:47:03 +080027
Martin Roth3316cf22012-12-05 16:22:54 -070028#if defined (CONFIG_HUDSON_IMC_FWM)
Alexandru Gagniuc01e0adf2014-03-29 17:07:26 -050029#include <Proc/Fch/FchPlatform.h>
Martin Roth3316cf22012-12-05 16:22:54 -070030
31static int bus_claimed = 0;
32#endif
33
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050034#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 Bao7bcffa52012-11-28 11:36:52 +080046static u32 spibar;
47
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050048static inline uint8_t spi_read(uint8_t reg)
49{
50 return read8(spibar + reg);
51}
52
53static inline void spi_write(uint8_t reg, uint8_t val)
54{
55 write8(spibar + reg, val);
56}
57
Zheng Bao7bcffa52012-11-28 11:36:52 +080058static void reset_internal_fifo_pointer(void)
zbao246e84b2012-07-13 18:47:03 +080059{
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050060 uint8_t reg8;
61
zbao246e84b2012-07-13 18:47:03 +080062 do {
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050063 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);
zbao246e84b2012-07-13 18:47:03 +080067}
68
Zheng Bao7bcffa52012-11-28 11:36:52 +080069static void execute_command(void)
zbao246e84b2012-07-13 18:47:03 +080070{
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050071 uint8_t reg8;
Zheng Bao7bcffa52012-11-28 11:36:52 +080072
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050073 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));
zbao246e84b2012-07-13 18:47:03 +080079}
80
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050081void spi_init(void)
zbao246e84b2012-07-13 18:47:03 +080082{
Zheng Bao7bcffa52012-11-28 11:36:52 +080083 device_t dev;
84
85 dev = dev_find_slot(0, PCI_DEVFN(0x14, 3));
86 spibar = pci_read_config32(dev, 0xA0) & ~0x1F;
zbao246e84b2012-07-13 18:47:03 +080087}
88
Zheng Bao7bcffa52012-11-28 11:36:52 +080089int spi_xfer(struct spi_slave *slave, const void *dout,
90 unsigned int bitsout, void *din, unsigned int bitsin)
zbao246e84b2012-07-13 18:47:03 +080091{
Zheng Bao7bcffa52012-11-28 11:36:52 +080092 /* First byte is cmd which can not being sent through FIFO. */
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050093 uint8_t cmd = *(uint8_t *)dout++;
94 uint8_t readoffby1;
Siyuan Wang91571452013-07-09 17:32:42 +080095#if !CONFIG_SOUTHBRIDGE_AMD_AGESA_YANGTZE
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050096 uint8_t readwrite;
Siyuan Wang91571452013-07-09 17:32:42 +080097#endif
Alexandru Gagniuc991e9512014-04-19 22:21:27 -050098 uint8_t bytesout, bytesin;
99 uint8_t count;
zbao246e84b2012-07-13 18:47:03 +0800100
Zheng Bao7bcffa52012-11-28 11:36:52 +0800101 bitsout -= 8;
102 bytesout = bitsout / 8;
103 bytesin = bitsin / 8;
zbao246e84b2012-07-13 18:47:03 +0800104
Zheng Bao7bcffa52012-11-28 11:36:52 +0800105 readoffby1 = bytesout ? 0 : 1;
zbao246e84b2012-07-13 18:47:03 +0800106
Siyuan Wang91571452013-07-09 17:32:42 +0800107#if CONFIG_SOUTHBRIDGE_AMD_AGESA_YANGTZE
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500108 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 Wang91571452013-07-09 17:32:42 +0800112#else
Zheng Bao7bcffa52012-11-28 11:36:52 +0800113 readwrite = (bytesin + readoffby1) << 4 | bytesout;
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500114 spi_write(SPI_REG_CNTRL01, readwrite);
Siyuan Wang91571452013-07-09 17:32:42 +0800115#endif
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500116 spi_write(SPI_REG_OPCODE, cmd);
zbao246e84b2012-07-13 18:47:03 +0800117
Zheng Bao7bcffa52012-11-28 11:36:52 +0800118 reset_internal_fifo_pointer();
119 for (count = 0; count < bytesout; count++, dout++) {
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500120 spi_write(SPI_REG_FIFO, *(uint8_t *)dout);
zbao246e84b2012-07-13 18:47:03 +0800121 }
Zheng Bao7bcffa52012-11-28 11:36:52 +0800122
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 Gagniuc991e9512014-04-19 22:21:27 -0500129 cmd = spi_read(SPI_REG_FIFO);
Zheng Bao7bcffa52012-11-28 11:36:52 +0800130 }
131
132 reset_internal_fifo_pointer();
133 for (count = 0; count < bytesin; count++, din++) {
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500134 *(uint8_t *)din = spi_read(SPI_REG_FIFO);
Zheng Bao7bcffa52012-11-28 11:36:52 +0800135 }
136
137 return 0;
138}
139int spi_claim_bus(struct spi_slave *slave)
140{
Martin Roth3316cf22012-12-05 16:22:54 -0700141#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 Bao7bcffa52012-11-28 11:36:52 +0800150 return 0;
zbao246e84b2012-07-13 18:47:03 +0800151}
152
Zheng Bao7bcffa52012-11-28 11:36:52 +0800153void spi_release_bus(struct spi_slave *slave)
zbao246e84b2012-07-13 18:47:03 +0800154{
Martin Roth3316cf22012-12-05 16:22:54 -0700155#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
zbao246e84b2012-07-13 18:47:03 +0800165}
166
Zheng Bao7bcffa52012-11-28 11:36:52 +0800167void spi_cs_activate(struct spi_slave *slave)
zbao246e84b2012-07-13 18:47:03 +0800168{
Zheng Bao7bcffa52012-11-28 11:36:52 +0800169}
170
171void spi_cs_deactivate(struct spi_slave *slave)
172{
173}
174
175struct 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;
zbao246e84b2012-07-13 18:47:03 +0800187}