blob: 3b54490117825e8a935aa3ee7d9683ddaecf8303 [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
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>
zbao246e84b2012-07-13 18:47:03 +080022#include <arch/io.h>
Zheng Bao7bcffa52012-11-28 11:36:52 +080023#include <spi.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
Zheng Bao7bcffa52012-11-28 11:36:52 +080028static u32 spibar;
29
30static void reset_internal_fifo_pointer(void)
zbao246e84b2012-07-13 18:47:03 +080031{
zbao246e84b2012-07-13 18:47:03 +080032 do {
Zheng Bao7bcffa52012-11-28 11:36:52 +080033 write8(spibar + 2, read8(spibar + 2) | 0x10);
34 } while (read8(spibar + 0xD) & 0x7);
zbao246e84b2012-07-13 18:47:03 +080035}
36
Zheng Bao7bcffa52012-11-28 11:36:52 +080037static void execute_command(void)
zbao246e84b2012-07-13 18:47:03 +080038{
Zheng Bao7bcffa52012-11-28 11:36:52 +080039 write8(spibar + 2, read8(spibar + 2) | 1);
40
41 while ((read8(spibar + 2) & 1) && (read8(spibar+3) & 0x80));
zbao246e84b2012-07-13 18:47:03 +080042}
43
Zheng Bao7bcffa52012-11-28 11:36:52 +080044void spi_init()
zbao246e84b2012-07-13 18:47:03 +080045{
Zheng Bao7bcffa52012-11-28 11:36:52 +080046 device_t dev;
47
48 dev = dev_find_slot(0, PCI_DEVFN(0x14, 3));
49 spibar = pci_read_config32(dev, 0xA0) & ~0x1F;
zbao246e84b2012-07-13 18:47:03 +080050}
51
Zheng Bao7bcffa52012-11-28 11:36:52 +080052int spi_xfer(struct spi_slave *slave, const void *dout,
53 unsigned int bitsout, void *din, unsigned int bitsin)
zbao246e84b2012-07-13 18:47:03 +080054{
Zheng Bao7bcffa52012-11-28 11:36:52 +080055 /* First byte is cmd which can not being sent through FIFO. */
56 u8 cmd = *(u8 *)dout++;
57 u8 readoffby1;
58 u8 readwrite;
59 u8 bytesout, bytesin;
60 u8 count;
zbao246e84b2012-07-13 18:47:03 +080061
Zheng Bao7bcffa52012-11-28 11:36:52 +080062 bitsout -= 8;
63 bytesout = bitsout / 8;
64 bytesin = bitsin / 8;
zbao246e84b2012-07-13 18:47:03 +080065
Zheng Bao7bcffa52012-11-28 11:36:52 +080066 readoffby1 = bytesout ? 0 : 1;
zbao246e84b2012-07-13 18:47:03 +080067
Zheng Bao7bcffa52012-11-28 11:36:52 +080068 readwrite = (bytesin + readoffby1) << 4 | bytesout;
69 write8(spibar + 1, readwrite);
70 write8(spibar + 0, cmd);
zbao246e84b2012-07-13 18:47:03 +080071
Zheng Bao7bcffa52012-11-28 11:36:52 +080072 reset_internal_fifo_pointer();
73 for (count = 0; count < bytesout; count++, dout++) {
74 write8(spibar + 0x0C, *(u8 *)dout);
zbao246e84b2012-07-13 18:47:03 +080075 }
Zheng Bao7bcffa52012-11-28 11:36:52 +080076
77 reset_internal_fifo_pointer();
78 execute_command();
79
80 reset_internal_fifo_pointer();
81 /* Skip the bytes we sent. */
82 for (count = 0; count < bytesout; count++) {
83 cmd = read8(spibar + 0x0C);
84 }
85
86 reset_internal_fifo_pointer();
87 for (count = 0; count < bytesin; count++, din++) {
88 *(u8 *)din = read8(spibar + 0x0C);
89 }
90
91 return 0;
92}
93int spi_claim_bus(struct spi_slave *slave)
94{
95 return 0;
zbao246e84b2012-07-13 18:47:03 +080096}
97
Zheng Bao7bcffa52012-11-28 11:36:52 +080098void spi_release_bus(struct spi_slave *slave)
zbao246e84b2012-07-13 18:47:03 +080099{
zbao246e84b2012-07-13 18:47:03 +0800100}
101
Zheng Bao7bcffa52012-11-28 11:36:52 +0800102void spi_cs_activate(struct spi_slave *slave)
zbao246e84b2012-07-13 18:47:03 +0800103{
Zheng Bao7bcffa52012-11-28 11:36:52 +0800104}
105
106void spi_cs_deactivate(struct spi_slave *slave)
107{
108}
109
110struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
111 unsigned int max_hz, unsigned int mode)
112{
113 struct spi_slave *slave = malloc(sizeof(*slave));
114
115 if (!slave) {
116 return NULL;
117 }
118
119 memset(slave, 0, sizeof(*slave));
120
121 return slave;
zbao246e84b2012-07-13 18:47:03 +0800122}