blob: 48820bc2bb5ff996202ff4ba59bbf89fd8608240 [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 {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080043 write8((void *)(spibar + 2),
44 read8((void *)(spibar + 2)) | 0x10);
45 } while (read8((void *)(spibar + 0xD)) & 0x7);
zbao01bd79f2012-03-23 11:36:08 +080046}
47
Zheng Bao7bcffa52012-11-28 11:36:52 +080048static void execute_command(void)
zbao01bd79f2012-03-23 11:36:08 +080049{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080050 write8((void *)(spibar + 2), read8((void *)(spibar + 2)) | 1);
Zheng Bao7bcffa52012-11-28 11:36:52 +080051
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080052 while ((read8((void *)(spibar + 2)) & 1) &&
53 (read8((void *)(spibar+3)) & 0x80));
zbao01bd79f2012-03-23 11:36:08 +080054}
55
Zheng Bao7bcffa52012-11-28 11:36:52 +080056void spi_init()
zbao01bd79f2012-03-23 11:36:08 +080057{
Zheng Bao7bcffa52012-11-28 11:36:52 +080058 device_t dev;
59
60 dev = dev_find_slot(0, PCI_DEVFN(0x14, 3));
61 spibar = pci_read_config32(dev, 0xA0) & ~0x1F;
zbao01bd79f2012-03-23 11:36:08 +080062}
63
Kyösti Mälkki11104952014-06-29 16:17:33 +030064unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
65{
66 return min(AMD_SB_SPI_TX_LEN - cmd_len, buf_len);
67}
68
Zheng Bao7bcffa52012-11-28 11:36:52 +080069int spi_xfer(struct spi_slave *slave, const void *dout,
Gabe Black93d9f922014-03-27 21:52:43 -070070 unsigned int bytesout, void *din, unsigned int bytesin)
zbao01bd79f2012-03-23 11:36:08 +080071{
Zheng Bao7bcffa52012-11-28 11:36:52 +080072 /* First byte is cmd which can not being sent through FIFO. */
73 u8 cmd = *(u8 *)dout++;
74 u8 readoffby1;
75 u8 readwrite;
Zheng Bao7bcffa52012-11-28 11:36:52 +080076 u8 count;
zbao01bd79f2012-03-23 11:36:08 +080077
Gabe Black93d9f922014-03-27 21:52:43 -070078 bytesout--;
zbao01bd79f2012-03-23 11:36:08 +080079
Kyösti Mälkki9f0a2be2014-06-30 07:34:36 +030080 /*
81 * Check if this is a write command attempting to transfer more bytes
82 * than the controller can handle. Iterations for writes are not
83 * supported here because each SPI write command needs to be preceded
84 * and followed by other SPI commands, and this sequence is controlled
85 * by the SPI chip driver.
86 */
87 if (bytesout > AMD_SB_SPI_TX_LEN) {
88 printk(BIOS_DEBUG, "FCH SPI: Too much to write. Does your SPI chip driver use"
89 " spi_crop_chunk()?\n");
90 return -1;
91 }
92
Zheng Bao7bcffa52012-11-28 11:36:52 +080093 readoffby1 = bytesout ? 0 : 1;
zbao01bd79f2012-03-23 11:36:08 +080094
Zheng Bao7bcffa52012-11-28 11:36:52 +080095 readwrite = (bytesin + readoffby1) << 4 | bytesout;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080096 write8((void *)(spibar + 1), readwrite);
97 write8((void *)(spibar + 0), cmd);
zbao01bd79f2012-03-23 11:36:08 +080098
Zheng Bao7bcffa52012-11-28 11:36:52 +080099 reset_internal_fifo_pointer();
100 for (count = 0; count < bytesout; count++, dout++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800101 write8((void *)(spibar + 0x0C), *(u8 *)dout);
zbao01bd79f2012-03-23 11:36:08 +0800102 }
Zheng Bao7bcffa52012-11-28 11:36:52 +0800103
104 reset_internal_fifo_pointer();
105 execute_command();
106
107 reset_internal_fifo_pointer();
108 /* Skip the bytes we sent. */
109 for (count = 0; count < bytesout; count++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800110 cmd = read8((void *)(spibar + 0x0C));
Zheng Bao7bcffa52012-11-28 11:36:52 +0800111 }
112
113 reset_internal_fifo_pointer();
114 for (count = 0; count < bytesin; count++, din++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800115 *(u8 *)din = read8((void *)(spibar + 0x0C));
Zheng Bao7bcffa52012-11-28 11:36:52 +0800116 }
117
118 return 0;
119}
Martin Roth3316cf22012-12-05 16:22:54 -0700120
Dave Frodin9b800ae2014-06-11 13:15:56 -0600121#if IS_ENABLED (CONFIG_SB800_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -0700122
123static void ImcSleep(void)
124{
125 u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */
126 u8 reg0_val = 0; /* clear response register */
127 u8 reg1_val = 0xB4; /* request ownership flag */
128
129 WriteECmsg (MSG_REG0, AccWidthUint8, &reg0_val);
130 WriteECmsg (MSG_REG1, AccWidthUint8, &reg1_val);
131 WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val);
132
133 WaitForEcLDN9MailboxCmdAck();
134}
135
136
137static void ImcWakeup(void)
138{
139 u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */
Idwer Volleringd26da9c2013-12-22 21:38:18 +0000140 u8 reg0_val = 0; /* clear response register */
Martin Roth3316cf22012-12-05 16:22:54 -0700141 u8 reg1_val = 0xB5; /* release ownership flag */
142
143 WriteECmsg (MSG_REG0, AccWidthUint8, &reg0_val);
144 WriteECmsg (MSG_REG1, AccWidthUint8, &reg1_val);
145 WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val);
146
147 WaitForEcLDN9MailboxCmdAck();
148}
149#endif
150
Zheng Bao7bcffa52012-11-28 11:36:52 +0800151int spi_claim_bus(struct spi_slave *slave)
152{
Dave Frodin9b800ae2014-06-11 13:15:56 -0600153#if IS_ENABLED (CONFIG_SB800_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -0700154
155 if (slave->rw == SPI_WRITE_FLAG) {
156 bus_claimed++;
157 if (bus_claimed == 1)
158 ImcSleep();
159 }
160#endif
161
Zheng Bao7bcffa52012-11-28 11:36:52 +0800162 return 0;
zbao01bd79f2012-03-23 11:36:08 +0800163}
164
Zheng Bao7bcffa52012-11-28 11:36:52 +0800165void spi_release_bus(struct spi_slave *slave)
zbao01bd79f2012-03-23 11:36:08 +0800166{
Dave Frodin9b800ae2014-06-11 13:15:56 -0600167#if IS_ENABLED (CONFIG_SB800_IMC_FWM)
Martin Roth3316cf22012-12-05 16:22:54 -0700168
169 if (slave->rw == SPI_WRITE_FLAG) {
170 bus_claimed--;
171 if (bus_claimed <= 0) {
172 bus_claimed = 0;
173 ImcWakeup();
174 }
175 }
176#endif
zbao01bd79f2012-03-23 11:36:08 +0800177}
178
Gabe Black1e187352014-03-27 20:37:03 -0700179struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
Zheng Bao7bcffa52012-11-28 11:36:52 +0800180{
181 struct spi_slave *slave = malloc(sizeof(*slave));
182
183 if (!slave) {
184 return NULL;
185 }
186
187 memset(slave, 0, sizeof(*slave));
188
189 return slave;
zbao01bd79f2012-03-23 11:36:08 +0800190}