blob: 2c541e352e06fe048db044d4c15d9f8a7bd74ca6 [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.
zbao01bd79f2012-03-23 11:36:08 +080014 */
Zheng Bao7bcffa52012-11-28 11:36:52 +080015#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
zbao01bd79f2012-03-23 11:36:08 +080018#include <arch/io.h>
Kyösti Mälkki9f0a2be2014-06-30 07:34:36 +030019#include <console/console.h>
Furquan Shaikhc28984d2016-11-20 21:04:00 -080020#include <spi_flash.h>
Zheng Bao600784e2013-02-07 17:30:23 +080021#include <spi-generic.h>
zbao01bd79f2012-03-23 11:36:08 +080022#include <device/device.h>
Zheng Bao7bcffa52012-11-28 11:36:52 +080023#include <device/pci.h>
24#include <device/pci_ops.h>
zbao01bd79f2012-03-23 11:36:08 +080025
Martin Roth3316cf22012-12-05 16:22:54 -070026#include "SBPLATFORM.h"
27#include <vendorcode/amd/cimx/sb800/ECfan.h>
28
Kyösti Mälkki11104952014-06-29 16:17:33 +030029#define AMD_SB_SPI_TX_LEN 8
30
Stefan Reinauer12bce3f2015-06-18 01:17:38 -070031static uintptr_t spibar;
zbao01bd79f2012-03-23 11:36:08 +080032
Zheng Bao7bcffa52012-11-28 11:36:52 +080033static void reset_internal_fifo_pointer(void)
zbao01bd79f2012-03-23 11:36:08 +080034{
zbao01bd79f2012-03-23 11:36:08 +080035 do {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080036 write8((void *)(spibar + 2),
37 read8((void *)(spibar + 2)) | 0x10);
38 } while (read8((void *)(spibar + 0xD)) & 0x7);
zbao01bd79f2012-03-23 11:36:08 +080039}
40
Zheng Bao7bcffa52012-11-28 11:36:52 +080041static void execute_command(void)
zbao01bd79f2012-03-23 11:36:08 +080042{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080043 write8((void *)(spibar + 2), read8((void *)(spibar + 2)) | 1);
Zheng Bao7bcffa52012-11-28 11:36:52 +080044
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080045 while ((read8((void *)(spibar + 2)) & 1) &&
46 (read8((void *)(spibar+3)) & 0x80));
zbao01bd79f2012-03-23 11:36:08 +080047}
48
Zheng Bao7bcffa52012-11-28 11:36:52 +080049void spi_init()
zbao01bd79f2012-03-23 11:36:08 +080050{
Zheng Bao7bcffa52012-11-28 11:36:52 +080051 device_t dev;
52
53 dev = dev_find_slot(0, PCI_DEVFN(0x14, 3));
54 spibar = pci_read_config32(dev, 0xA0) & ~0x1F;
zbao01bd79f2012-03-23 11:36:08 +080055}
56
Furquan Shaikh94f86992016-12-01 07:12:32 -080057static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
Furquan Shaikh0dba0252016-11-30 04:34:22 -080058 size_t bytesout, void *din, size_t bytesin)
zbao01bd79f2012-03-23 11:36:08 +080059{
Zheng Bao7bcffa52012-11-28 11:36:52 +080060 /* First byte is cmd which can not being sent through FIFO. */
61 u8 cmd = *(u8 *)dout++;
62 u8 readoffby1;
63 u8 readwrite;
Furquan Shaikh0dba0252016-11-30 04:34:22 -080064 size_t count;
zbao01bd79f2012-03-23 11:36:08 +080065
Gabe Black93d9f922014-03-27 21:52:43 -070066 bytesout--;
zbao01bd79f2012-03-23 11:36:08 +080067
Kyösti Mälkki9f0a2be2014-06-30 07:34:36 +030068 /*
69 * Check if this is a write command attempting to transfer more bytes
70 * than the controller can handle. Iterations for writes are not
71 * supported here because each SPI write command needs to be preceded
72 * and followed by other SPI commands, and this sequence is controlled
73 * by the SPI chip driver.
74 */
75 if (bytesout > AMD_SB_SPI_TX_LEN) {
76 printk(BIOS_DEBUG, "FCH SPI: Too much to write. Does your SPI chip driver use"
77 " spi_crop_chunk()?\n");
78 return -1;
79 }
80
Zheng Bao7bcffa52012-11-28 11:36:52 +080081 readoffby1 = bytesout ? 0 : 1;
zbao01bd79f2012-03-23 11:36:08 +080082
Zheng Bao7bcffa52012-11-28 11:36:52 +080083 readwrite = (bytesin + readoffby1) << 4 | bytesout;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080084 write8((void *)(spibar + 1), readwrite);
85 write8((void *)(spibar + 0), cmd);
zbao01bd79f2012-03-23 11:36:08 +080086
Zheng Bao7bcffa52012-11-28 11:36:52 +080087 reset_internal_fifo_pointer();
88 for (count = 0; count < bytesout; count++, dout++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080089 write8((void *)(spibar + 0x0C), *(u8 *)dout);
zbao01bd79f2012-03-23 11:36:08 +080090 }
Zheng Bao7bcffa52012-11-28 11:36:52 +080091
92 reset_internal_fifo_pointer();
93 execute_command();
94
95 reset_internal_fifo_pointer();
96 /* Skip the bytes we sent. */
97 for (count = 0; count < bytesout; count++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080098 cmd = read8((void *)(spibar + 0x0C));
Zheng Bao7bcffa52012-11-28 11:36:52 +080099 }
100
101 reset_internal_fifo_pointer();
102 for (count = 0; count < bytesin; count++, din++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800103 *(u8 *)din = read8((void *)(spibar + 0x0C));
Zheng Bao7bcffa52012-11-28 11:36:52 +0800104 }
105
106 return 0;
107}
Martin Roth3316cf22012-12-05 16:22:54 -0700108
Martin Roth3316cf22012-12-05 16:22:54 -0700109static void ImcSleep(void)
110{
111 u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */
112 u8 reg0_val = 0; /* clear response register */
113 u8 reg1_val = 0xB4; /* request ownership flag */
114
115 WriteECmsg (MSG_REG0, AccWidthUint8, &reg0_val);
116 WriteECmsg (MSG_REG1, AccWidthUint8, &reg1_val);
117 WriteECmsg (MSG_SYS_TO_IMC, AccWidthUint8, &cmd_val);
118
119 WaitForEcLDN9MailboxCmdAck();
120}
121
122
123static void ImcWakeup(void)
124{
125 u8 cmd_val = 0x96; /* Kick off IMC Mailbox command 96 */
Idwer Volleringd26da9c2013-12-22 21:38:18 +0000126 u8 reg0_val = 0; /* clear response register */
Martin Roth3316cf22012-12-05 16:22:54 -0700127 u8 reg1_val = 0xB5; /* release 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}
Martin Roth3316cf22012-12-05 16:22:54 -0700135
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800136int chipset_volatile_group_begin(const struct spi_flash *flash)
137{
138 if (!IS_ENABLED(CONFIG_SB800_IMC_FWM))
139 return 0;
140
141 ImcSleep();
142 return 0;
143}
144
145int chipset_volatile_group_end(const struct spi_flash *flash)
146{
147 if (!IS_ENABLED(CONFIG_SB800_IMC_FWM))
148 return 0;
149
150 ImcWakeup();
151 return 0;
zbao01bd79f2012-03-23 11:36:08 +0800152}
153
Aaron Durbin851dde82018-04-19 21:15:25 -0600154static int xfer_vectors(const struct spi_slave *slave,
155 struct spi_op vectors[], size_t count)
156{
157 return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
158}
159
Furquan Shaikh94f86992016-12-01 07:12:32 -0800160static const struct spi_ctrlr spi_ctrlr = {
Aaron Durbin851dde82018-04-19 21:15:25 -0600161 .xfer_vector = xfer_vectors,
Furquan Shaikhde705fa2017-04-19 19:27:28 -0700162 .max_xfer_size = AMD_SB_SPI_TX_LEN,
Aaron Durbin1fcc9f32018-01-29 11:30:17 -0700163 .flags = SPI_CNTRLR_DEDUCT_CMD_LEN,
Furquan Shaikh94f86992016-12-01 07:12:32 -0800164};
165
Furquan Shaikh12eca762017-05-18 14:58:49 -0700166const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
167 {
168 .ctrlr = &spi_ctrlr,
169 .bus_start = 0,
170 .bus_end = 0,
171 },
172};
173
174const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);