blob: f38e691254489dbc3663e3e909d715b2f6393bed [file] [log] [blame]
zbao01bd79f2012-03-23 11:36:08 +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
zbao01bd79f2012-03-23 11:36:08 +080018 */
Zheng Bao7bcffa52012-11-28 11:36:52 +080019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
zbao01bd79f2012-03-23 11:36:08 +080022#include <arch/io.h>
Kyösti Mälkki9f0a2be2014-06-30 07:34:36 +030023#include <console/console.h>
Zheng Bao600784e2013-02-07 17:30:23 +080024#include <spi-generic.h>
zbao01bd79f2012-03-23 11:36:08 +080025#include <device/device.h>
Zheng Bao7bcffa52012-11-28 11:36:52 +080026#include <device/pci.h>
27#include <device/pci_ops.h>
zbao01bd79f2012-03-23 11:36:08 +080028
Dave Frodin9b800ae2014-06-11 13:15:56 -060029#if IS_ENABLED (CONFIG_SB800_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -070030#include "SBPLATFORM.h"
31#include <vendorcode/amd/cimx/sb800/ECfan.h>
32
33static int bus_claimed = 0;
34#endif
35
Kyösti Mälkki11104952014-06-29 16:17:33 +030036#define AMD_SB_SPI_TX_LEN 8
37
Zheng Bao7bcffa52012-11-28 11:36:52 +080038static u32 spibar;
zbao01bd79f2012-03-23 11:36:08 +080039
Zheng Bao7bcffa52012-11-28 11:36:52 +080040static void reset_internal_fifo_pointer(void)
zbao01bd79f2012-03-23 11:36:08 +080041{
zbao01bd79f2012-03-23 11:36:08 +080042 do {
Zheng Bao7bcffa52012-11-28 11:36:52 +080043 write8(spibar + 2, read8(spibar + 2) | 0x10);
44 } while (read8(spibar + 0xD) & 0x7);
zbao01bd79f2012-03-23 11:36:08 +080045}
46
Zheng Bao7bcffa52012-11-28 11:36:52 +080047static void execute_command(void)
zbao01bd79f2012-03-23 11:36:08 +080048{
Zheng Bao7bcffa52012-11-28 11:36:52 +080049 write8(spibar + 2, read8(spibar + 2) | 1);
50
51 while ((read8(spibar + 2) & 1) && (read8(spibar+3) & 0x80));
zbao01bd79f2012-03-23 11:36:08 +080052}
53
Zheng Bao7bcffa52012-11-28 11:36:52 +080054void spi_init()
zbao01bd79f2012-03-23 11:36:08 +080055{
Zheng Bao7bcffa52012-11-28 11:36:52 +080056 device_t dev;
57
58 dev = dev_find_slot(0, PCI_DEVFN(0x14, 3));
59 spibar = pci_read_config32(dev, 0xA0) & ~0x1F;
zbao01bd79f2012-03-23 11:36:08 +080060}
61
Kyösti Mälkki11104952014-06-29 16:17:33 +030062unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
63{
64 return min(AMD_SB_SPI_TX_LEN - cmd_len, buf_len);
65}
66
Zheng Bao7bcffa52012-11-28 11:36:52 +080067int spi_xfer(struct spi_slave *slave, const void *dout,
Gabe Black93d9f922014-03-27 21:52:43 -070068 unsigned int bytesout, void *din, unsigned int bytesin)
zbao01bd79f2012-03-23 11:36:08 +080069{
Zheng Bao7bcffa52012-11-28 11:36:52 +080070 /* First byte is cmd which can not being sent through FIFO. */
71 u8 cmd = *(u8 *)dout++;
72 u8 readoffby1;
73 u8 readwrite;
Zheng Bao7bcffa52012-11-28 11:36:52 +080074 u8 count;
zbao01bd79f2012-03-23 11:36:08 +080075
Gabe Black93d9f922014-03-27 21:52:43 -070076 bytesout--;
zbao01bd79f2012-03-23 11:36:08 +080077
Kyösti Mälkki9f0a2be2014-06-30 07:34:36 +030078 /*
79 * Check if this is a write command attempting to transfer more bytes
80 * than the controller can handle. Iterations for writes are not
81 * supported here because each SPI write command needs to be preceded
82 * and followed by other SPI commands, and this sequence is controlled
83 * by the SPI chip driver.
84 */
85 if (bytesout > AMD_SB_SPI_TX_LEN) {
86 printk(BIOS_DEBUG, "FCH SPI: Too much to write. Does your SPI chip driver use"
87 " spi_crop_chunk()?\n");
88 return -1;
89 }
90
Zheng Bao7bcffa52012-11-28 11:36:52 +080091 readoffby1 = bytesout ? 0 : 1;
zbao01bd79f2012-03-23 11:36:08 +080092
Zheng Bao7bcffa52012-11-28 11:36:52 +080093 readwrite = (bytesin + readoffby1) << 4 | bytesout;
94 write8(spibar + 1, readwrite);
95 write8(spibar + 0, cmd);
zbao01bd79f2012-03-23 11:36:08 +080096
Zheng Bao7bcffa52012-11-28 11:36:52 +080097 reset_internal_fifo_pointer();
98 for (count = 0; count < bytesout; count++, dout++) {
99 write8(spibar + 0x0C, *(u8 *)dout);
zbao01bd79f2012-03-23 11:36:08 +0800100 }
Zheng Bao7bcffa52012-11-28 11:36:52 +0800101
102 reset_internal_fifo_pointer();
103 execute_command();
104
105 reset_internal_fifo_pointer();
106 /* Skip the bytes we sent. */
107 for (count = 0; count < bytesout; count++) {
108 cmd = read8(spibar + 0x0C);
109 }
110
111 reset_internal_fifo_pointer();
112 for (count = 0; count < bytesin; count++, din++) {
113 *(u8 *)din = read8(spibar + 0x0C);
114 }
115
116 return 0;
117}
Martin Roth3316cf22012-12-05 16:22:54 -0700118
Dave Frodin9b800ae2014-06-11 13:15:56 -0600119#if IS_ENABLED (CONFIG_SB800_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -0700120
121static void ImcSleep(void)
122{
123 u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */
124 u8 reg0_val = 0; /* clear response register */
125 u8 reg1_val = 0xB4; /* request ownership flag */
126
127 WriteECmsg (MSG_REG0, AccWidthUint8, &reg0_val);
128 WriteECmsg (MSG_REG1, AccWidthUint8, &reg1_val);
129 WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val);
130
131 WaitForEcLDN9MailboxCmdAck();
132}
133
134
135static void ImcWakeup(void)
136{
137 u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */
Idwer Volleringd26da9c2013-12-22 21:38:18 +0000138 u8 reg0_val = 0; /* clear response register */
Martin Roth3316cf22012-12-05 16:22:54 -0700139 u8 reg1_val = 0xB5; /* release ownership flag */
140
141 WriteECmsg (MSG_REG0, AccWidthUint8, &reg0_val);
142 WriteECmsg (MSG_REG1, AccWidthUint8, &reg1_val);
143 WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val);
144
145 WaitForEcLDN9MailboxCmdAck();
146}
147#endif
148
Zheng Bao7bcffa52012-11-28 11:36:52 +0800149int spi_claim_bus(struct spi_slave *slave)
150{
Dave Frodin9b800ae2014-06-11 13:15:56 -0600151#if IS_ENABLED (CONFIG_SB800_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -0700152
153 if (slave->rw == SPI_WRITE_FLAG) {
154 bus_claimed++;
155 if (bus_claimed == 1)
156 ImcSleep();
157 }
158#endif
159
Zheng Bao7bcffa52012-11-28 11:36:52 +0800160 return 0;
zbao01bd79f2012-03-23 11:36:08 +0800161}
162
Zheng Bao7bcffa52012-11-28 11:36:52 +0800163void spi_release_bus(struct spi_slave *slave)
zbao01bd79f2012-03-23 11:36:08 +0800164{
Dave Frodin9b800ae2014-06-11 13:15:56 -0600165#if IS_ENABLED (CONFIG_SB800_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -0700166
167 if (slave->rw == SPI_WRITE_FLAG) {
168 bus_claimed--;
169 if (bus_claimed <= 0) {
170 bus_claimed = 0;
171 ImcWakeup();
172 }
173 }
174#endif
zbao01bd79f2012-03-23 11:36:08 +0800175}
176
Zheng Bao7bcffa52012-11-28 11:36:52 +0800177void spi_cs_activate(struct spi_slave *slave)
zbao01bd79f2012-03-23 11:36:08 +0800178{
Zheng Bao7bcffa52012-11-28 11:36:52 +0800179}
180
181void spi_cs_deactivate(struct spi_slave *slave)
182{
183}
184
Gabe Black1e187352014-03-27 20:37:03 -0700185struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
Zheng Bao7bcffa52012-11-28 11:36:52 +0800186{
187 struct spi_slave *slave = malloc(sizeof(*slave));
188
189 if (!slave) {
190 return NULL;
191 }
192
193 memset(slave, 0, sizeof(*slave));
194
195 return slave;
zbao01bd79f2012-03-23 11:36:08 +0800196}