blob: d4b80f96277d4a12fa2677b71a1a81efaf8976cf [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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
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>
Zheng Bao600784e2013-02-07 17:30:23 +080023#include <spi-generic.h>
zbao01bd79f2012-03-23 11:36:08 +080024#include <device/device.h>
Zheng Bao7bcffa52012-11-28 11:36:52 +080025#include <device/pci.h>
26#include <device/pci_ops.h>
zbao01bd79f2012-03-23 11:36:08 +080027
Martin Roth3316cf22012-12-05 16:22:54 -070028#if defined (CONFIG_SB800_IMC_FWM)
29#include "SBPLATFORM.h"
30#include <vendorcode/amd/cimx/sb800/ECfan.h>
31
32static int bus_claimed = 0;
33#endif
34
Zheng Bao7bcffa52012-11-28 11:36:52 +080035static u32 spibar;
zbao01bd79f2012-03-23 11:36:08 +080036
Zheng Bao7bcffa52012-11-28 11:36:52 +080037static void reset_internal_fifo_pointer(void)
zbao01bd79f2012-03-23 11:36:08 +080038{
zbao01bd79f2012-03-23 11:36:08 +080039 do {
Zheng Bao7bcffa52012-11-28 11:36:52 +080040 write8(spibar + 2, read8(spibar + 2) | 0x10);
41 } while (read8(spibar + 0xD) & 0x7);
zbao01bd79f2012-03-23 11:36:08 +080042}
43
Zheng Bao7bcffa52012-11-28 11:36:52 +080044static void execute_command(void)
zbao01bd79f2012-03-23 11:36:08 +080045{
Zheng Bao7bcffa52012-11-28 11:36:52 +080046 write8(spibar + 2, read8(spibar + 2) | 1);
47
48 while ((read8(spibar + 2) & 1) && (read8(spibar+3) & 0x80));
zbao01bd79f2012-03-23 11:36:08 +080049}
50
Zheng Bao7bcffa52012-11-28 11:36:52 +080051void spi_init()
zbao01bd79f2012-03-23 11:36:08 +080052{
Zheng Bao7bcffa52012-11-28 11:36:52 +080053 device_t dev;
54
55 dev = dev_find_slot(0, PCI_DEVFN(0x14, 3));
56 spibar = pci_read_config32(dev, 0xA0) & ~0x1F;
zbao01bd79f2012-03-23 11:36:08 +080057}
58
Zheng Bao7bcffa52012-11-28 11:36:52 +080059int spi_xfer(struct spi_slave *slave, const void *dout,
60 unsigned int bitsout, void *din, unsigned int bitsin)
zbao01bd79f2012-03-23 11:36:08 +080061{
Zheng Bao7bcffa52012-11-28 11:36:52 +080062 /* First byte is cmd which can not being sent through FIFO. */
63 u8 cmd = *(u8 *)dout++;
64 u8 readoffby1;
65 u8 readwrite;
66 u8 bytesout, bytesin;
67 u8 count;
zbao01bd79f2012-03-23 11:36:08 +080068
Zheng Bao7bcffa52012-11-28 11:36:52 +080069 bitsout -= 8;
70 bytesout = bitsout / 8;
71 bytesin = bitsin / 8;
zbao01bd79f2012-03-23 11:36:08 +080072
Zheng Bao7bcffa52012-11-28 11:36:52 +080073 readoffby1 = bytesout ? 0 : 1;
zbao01bd79f2012-03-23 11:36:08 +080074
Zheng Bao7bcffa52012-11-28 11:36:52 +080075 readwrite = (bytesin + readoffby1) << 4 | bytesout;
76 write8(spibar + 1, readwrite);
77 write8(spibar + 0, cmd);
zbao01bd79f2012-03-23 11:36:08 +080078
Zheng Bao7bcffa52012-11-28 11:36:52 +080079 reset_internal_fifo_pointer();
80 for (count = 0; count < bytesout; count++, dout++) {
81 write8(spibar + 0x0C, *(u8 *)dout);
zbao01bd79f2012-03-23 11:36:08 +080082 }
Zheng Bao7bcffa52012-11-28 11:36:52 +080083
84 reset_internal_fifo_pointer();
85 execute_command();
86
87 reset_internal_fifo_pointer();
88 /* Skip the bytes we sent. */
89 for (count = 0; count < bytesout; count++) {
90 cmd = read8(spibar + 0x0C);
91 }
92
93 reset_internal_fifo_pointer();
94 for (count = 0; count < bytesin; count++, din++) {
95 *(u8 *)din = read8(spibar + 0x0C);
96 }
97
98 return 0;
99}
Martin Roth3316cf22012-12-05 16:22:54 -0700100
101#if defined (CONFIG_SB800_IMC_FWM)
102
103static void ImcSleep(void)
104{
105 u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */
106 u8 reg0_val = 0; /* clear response register */
107 u8 reg1_val = 0xB4; /* request ownership flag */
108
109 WriteECmsg (MSG_REG0, AccWidthUint8, &reg0_val);
110 WriteECmsg (MSG_REG1, AccWidthUint8, &reg1_val);
111 WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val);
112
113 WaitForEcLDN9MailboxCmdAck();
114}
115
116
117static void ImcWakeup(void)
118{
119 u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */
120 u8 reg0_val = 0;; /* clear response register */
121 u8 reg1_val = 0xB5; /* release ownership flag */
122
123 WriteECmsg (MSG_REG0, AccWidthUint8, &reg0_val);
124 WriteECmsg (MSG_REG1, AccWidthUint8, &reg1_val);
125 WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val);
126
127 WaitForEcLDN9MailboxCmdAck();
128}
129#endif
130
Zheng Bao7bcffa52012-11-28 11:36:52 +0800131int spi_claim_bus(struct spi_slave *slave)
132{
Martin Roth3316cf22012-12-05 16:22:54 -0700133#if defined (CONFIG_SB800_IMC_FWM)
134
135 if (slave->rw == SPI_WRITE_FLAG) {
136 bus_claimed++;
137 if (bus_claimed == 1)
138 ImcSleep();
139 }
140#endif
141
Zheng Bao7bcffa52012-11-28 11:36:52 +0800142 return 0;
zbao01bd79f2012-03-23 11:36:08 +0800143}
144
Zheng Bao7bcffa52012-11-28 11:36:52 +0800145void spi_release_bus(struct spi_slave *slave)
zbao01bd79f2012-03-23 11:36:08 +0800146{
Martin Roth3316cf22012-12-05 16:22:54 -0700147#if defined (CONFIG_SB800_IMC_FWM)
148
149 if (slave->rw == SPI_WRITE_FLAG) {
150 bus_claimed--;
151 if (bus_claimed <= 0) {
152 bus_claimed = 0;
153 ImcWakeup();
154 }
155 }
156#endif
zbao01bd79f2012-03-23 11:36:08 +0800157}
158
Zheng Bao7bcffa52012-11-28 11:36:52 +0800159void spi_cs_activate(struct spi_slave *slave)
zbao01bd79f2012-03-23 11:36:08 +0800160{
Zheng Bao7bcffa52012-11-28 11:36:52 +0800161}
162
163void spi_cs_deactivate(struct spi_slave *slave)
164{
165}
166
167struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
168 unsigned int max_hz, unsigned int mode)
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;
zbao01bd79f2012-03-23 11:36:08 +0800179}