summaryrefslogtreecommitdiff
path: root/src/southbridge/amd/cimx/sb800/spi.c
blob: b0ef753e109e28687514528d5e50ddbac98051e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2012 Advanced Micro Devices, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
#include <console/console.h>
#include <arch/io.h>
#include <device/device.h>
#include "SBPLATFORM.h"


void execute_command(volatile u8 * spi_address)
{
	*(spi_address + 2) |= 1;
}

void wait4command_complete(volatile u8 * spi_address)
{
	while (*(spi_address + 2) & 1)
		printk(BIOS_DEBUG, "wait4CommandComplete\n");
}

void reset_internal_fifo_pointer(volatile u8 * spi_address)
{
	u8 val;

	do {
		*(spi_address + 2) |= 0x10;
		val = *(spi_address + 0xd);
	} while (val & 0x7);
}

u8 read_spi_status(volatile u8 * spi_address)
{
	u8 val;
	*spi_address = 0x05;
	*(spi_address + 1) = 0x11;
	reset_internal_fifo_pointer(spi_address);
	*(spi_address + 0xC) = 0x0;	/* dummy */
	reset_internal_fifo_pointer(spi_address);
	execute_command(spi_address);
	wait4command_complete(spi_address);
	reset_internal_fifo_pointer(spi_address);
	val = *(spi_address + 0xC);
	val = *(spi_address + 0xC);
	return val;
}

void wait4flashpart_ready(volatile u8 * spi_address)
{
	while (read_spi_status(spi_address) & 1) ;
}

void write_spi_status(volatile u8 * spi_address, u8 status)
{
	*spi_address = 0x50;	/* EWSR */
	*(spi_address + 1) = 0;	/* RxByte=TxByte=0 */
	execute_command(spi_address);
	wait4command_complete(spi_address);

	*spi_address = 0x01;	/* WRSR */
	*(spi_address + 1) = 0x01;
	reset_internal_fifo_pointer(spi_address);
	*(spi_address + 0xC) = status;
	reset_internal_fifo_pointer(spi_address);
	execute_command(spi_address);
	wait4command_complete(spi_address);
	wait4flashpart_ready(spi_address);

	read_spi_status(spi_address);
}

void read_spi_id(volatile u8 * spi_address)
{
	u8 mid = 0, did = 0;
	*spi_address = 0x90;
	*(spi_address + 1) = 0x23;	/* RxByte=2, TxByte=3 */
	reset_internal_fifo_pointer(spi_address);
	*(spi_address + 0xC) = 0;
	*(spi_address + 0xC) = 0;
	*(spi_address + 0xC) = 0;
	reset_internal_fifo_pointer(spi_address);
	execute_command(spi_address);
	wait4command_complete(spi_address);
	reset_internal_fifo_pointer(spi_address);
	mid = *(spi_address + 0xC);
	printk(BIOS_DEBUG, "mid=%x, did=%x\n", mid, did);
	mid = *(spi_address + 0xC);
	printk(BIOS_DEBUG, "mid=%x, did=%x\n", mid, did);
	mid = *(spi_address + 0xC);
	printk(BIOS_DEBUG, "mid=%x, did=%x\n", mid, did);

	mid = *(spi_address + 0xC);
	did = *(spi_address + 0xC);
	printk(BIOS_DEBUG, "mid=%x, did=%x\n", mid, did);
}

void spi_write_enable(volatile u8 * spi_address)
{
	*spi_address = 0x06;	/* Write Enable */
	*(spi_address + 1) = 0x0;	/* RxByte=0, TxByte=0 */
	execute_command(spi_address);
	wait4command_complete(spi_address);
}

void sector_erase_spi(volatile u8 * spi_address, u32 address)
{
	spi_write_enable(spi_address);
	*spi_address = 0x20;
	*(spi_address + 1) = 0x03;	/* RxByte=0, TxByte=3 */

	reset_internal_fifo_pointer(spi_address);
	*(spi_address + 0xC) = (address >> 16) & 0xFF;
	*(spi_address + 0xC) = (address >> 8) & 0xFF;
	*(spi_address + 0xC) = (address >> 0) & 0xFF;
	reset_internal_fifo_pointer(spi_address);
	execute_command(spi_address);
	wait4command_complete(spi_address);
	wait4flashpart_ready(spi_address);
}

void chip_erase_spi(volatile u8 * spi_address)
{
	spi_write_enable(spi_address);
	*spi_address = 0xC7;
	*(spi_address + 1) = 0x00;
	execute_command(spi_address);
	wait4command_complete(spi_address);
	wait4flashpart_ready(spi_address);
}

void byte_program(volatile u8 * spi_address, u32 address, u32 data)
{
	spi_write_enable(spi_address);
	*spi_address = 0x02;
	*(spi_address + 1) = 0x0 << 4 | 4;
	reset_internal_fifo_pointer(spi_address);
	*(spi_address + 0xC) = (address >> 16) & 0xFF;
	*(spi_address + 0xC) = (address >> 8) & 0xFF;
	*(spi_address + 0xC) = (address >> 0) & 0xFF;
	*(spi_address + 0xC) = data & 0xFF;
	reset_internal_fifo_pointer(spi_address);
	execute_command(spi_address);
	wait4command_complete(spi_address);
	wait4flashpart_ready(spi_address);
}

void dword_noneAAI_program(volatile u8 * spi_address, u32 address, u32 data)
{
	u8 i;
	/*
	 * printk(BIOS_SPEW, "%s: addr=%x, data=%x\n", __func__, address, data);
	 */
	for (i = 0; i < 4; i++) {
		spi_write_enable(spi_address);
		*spi_address = 0x02;
		*(spi_address + 1) = 0x0 << 4 | 4;
		reset_internal_fifo_pointer(spi_address);
		*(spi_address + 0xC) = (address >> 16) & 0xFF;
		*(spi_address + 0xC) = (address >> 8) & 0xFF;
		*(spi_address + 0xC) = (address >> 0) & 0xFF;
		*(spi_address + 0xC) = data & 0xFF;
		data >>= 8;
		address++;
		reset_internal_fifo_pointer(spi_address);
		execute_command(spi_address);
		wait4command_complete(spi_address);
		wait4flashpart_ready(spi_address);
	}
}

void dword_program(volatile u8 * spi_address, u32 address, u32 data)
{
	spi_write_enable(spi_address);
	*spi_address = 0x02;
	*(spi_address + 1) = 0x0 << 4 | 7;
	reset_internal_fifo_pointer(spi_address);
	*(spi_address + 0xC) = (address >> 16) & 0xFF;
	*(spi_address + 0xC) = (address >> 8) & 0xFF;
	*(spi_address + 0xC) = (address >> 0) & 0xFF;
	*(spi_address + 0xC) = data & 0xFF;
	*(spi_address + 0xC) = (data >> 8) & 0xFF;
	*(spi_address + 0xC) = (data >> 16) & 0xFF;
	*(spi_address + 0xC) = (data >> 24) & 0xFF;
	reset_internal_fifo_pointer(spi_address);
	execute_command(spi_address);
	wait4command_complete(spi_address);
	wait4flashpart_ready(spi_address);
}

void direct_byte_program(volatile u8 * spi_address, volatile u32 * address, u32 data)
{
	spi_write_enable(spi_address);
	*address = data;
	wait4flashpart_ready(spi_address);
}