blob: 720b26f83e0245f41acfbaa841104538c10d6039 [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
Dave Frodin9b800ae2014-06-11 13:15:56 -060028#if IS_ENABLED (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,
Gabe Black93d9f922014-03-27 21:52:43 -070090 unsigned int bytesout, void *din, unsigned int bytesin)
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. */
Gabe Black93d9f922014-03-27 21:52:43 -070093 u8 cmd = *(u8 *)dout++;
94 u8 readoffby1;
95 u8 count;
zbao246e84b2012-07-13 18:47:03 +080096
Gabe Black93d9f922014-03-27 21:52:43 -070097 bytesout--;
Zheng Bao7bcffa52012-11-28 11:36:52 +080098 readoffby1 = bytesout ? 0 : 1;
zbao246e84b2012-07-13 18:47:03 +080099
Siyuan Wang91571452013-07-09 17:32:42 +0800100#if CONFIG_SOUTHBRIDGE_AMD_AGESA_YANGTZE
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500101 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 Wang91571452013-07-09 17:32:42 +0800105#else
Gabe Black93d9f922014-03-27 21:52:43 -0700106 u8 readwrite = (bytesin + readoffby1) << 4 | bytesout;
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500107 spi_write(SPI_REG_CNTRL01, readwrite);
Siyuan Wang91571452013-07-09 17:32:42 +0800108#endif
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500109 spi_write(SPI_REG_OPCODE, cmd);
zbao246e84b2012-07-13 18:47:03 +0800110
Zheng Bao7bcffa52012-11-28 11:36:52 +0800111 reset_internal_fifo_pointer();
112 for (count = 0; count < bytesout; count++, dout++) {
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500113 spi_write(SPI_REG_FIFO, *(uint8_t *)dout);
zbao246e84b2012-07-13 18:47:03 +0800114 }
Zheng Bao7bcffa52012-11-28 11:36:52 +0800115
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 Gagniuc991e9512014-04-19 22:21:27 -0500122 cmd = spi_read(SPI_REG_FIFO);
Zheng Bao7bcffa52012-11-28 11:36:52 +0800123 }
124
125 reset_internal_fifo_pointer();
126 for (count = 0; count < bytesin; count++, din++) {
Alexandru Gagniuc991e9512014-04-19 22:21:27 -0500127 *(uint8_t *)din = spi_read(SPI_REG_FIFO);
Zheng Bao7bcffa52012-11-28 11:36:52 +0800128 }
129
130 return 0;
131}
132int spi_claim_bus(struct spi_slave *slave)
133{
Dave Frodin9b800ae2014-06-11 13:15:56 -0600134#if IS_ENABLED (CONFIG_HUDSON_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -0700135
136 if (slave->rw == SPI_WRITE_FLAG) {
137 bus_claimed++;
138 if (bus_claimed == 1)
139 ImcSleep(NULL);
140 }
141#endif
142
Zheng Bao7bcffa52012-11-28 11:36:52 +0800143 return 0;
zbao246e84b2012-07-13 18:47:03 +0800144}
145
Zheng Bao7bcffa52012-11-28 11:36:52 +0800146void spi_release_bus(struct spi_slave *slave)
zbao246e84b2012-07-13 18:47:03 +0800147{
Dave Frodin9b800ae2014-06-11 13:15:56 -0600148#if IS_ENABLED (CONFIG_HUDSON_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -0700149
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
zbao246e84b2012-07-13 18:47:03 +0800158}
159
Zheng Bao7bcffa52012-11-28 11:36:52 +0800160void spi_cs_activate(struct spi_slave *slave)
zbao246e84b2012-07-13 18:47:03 +0800161{
Zheng Bao7bcffa52012-11-28 11:36:52 +0800162}
163
164void spi_cs_deactivate(struct spi_slave *slave)
165{
166}
167
Gabe Black1e187352014-03-27 20:37:03 -0700168struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
Zheng Bao7bcffa52012-11-28 11:36:52 +0800169{
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;
zbao246e84b2012-07-13 18:47:03 +0800179}