v2m_flash.c 4.54 KB
Newer Older
1
/*
Roberto Vargas's avatar
Roberto Vargas committed
2
 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
 */

#include <errno.h>
8
9
10

#include <drivers/cfi/v2m_flash.h>
#include <lib/mmio.h>
11

12
13
14
15
16
17
18
/*
 * This file supplies a low level interface to the vexpress NOR flash
 * memory of juno and fvp. This memory is organized as an interleaved
 * memory of two chips with a 16 bit word. It means that every 32 bit
 * access is going to access to two different chips. This is very
 * important when we send commands or read status of the chips.
 */
19
20
21
22
23
24
25

/*
 * DWS ready poll retries. The number of retries in this driver have been
 * obtained empirically from Juno. FVP implements a zero wait state NOR flash
 * model
 */
#define DWS_WORD_PROGRAM_RETRIES	1000
26
#define DWS_WORD_ERASE_RETRIES		3000000
27
#define DWS_WORD_LOCK_RETRIES		1000
28

Roberto Vargas's avatar
Roberto Vargas committed
29
30
31
/* Helper macro to detect end of command */
#define NOR_CMD_END (NOR_DWS | NOR_DWS << 16l)

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* Helper macros to access two flash banks in parallel */
#define NOR_2X16(d)			((d << 16) | (d & 0xffff))

static unsigned int nor_status(uintptr_t base_addr)
{
	unsigned long status;

	nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG);
	status = mmio_read_32(base_addr);
	status |= status >> 16; /* merge status from both flash banks */

	return status & 0xFFFF;
}

/*
 * Poll Write State Machine.
 * Return values:
49
50
51
 *    0      = WSM ready
 *    -EBUSY = WSM busy after the number of retries
 */
52
static int nor_poll_dws(uintptr_t base_addr, unsigned long int retries)
53
{
Roberto Vargas's avatar
Roberto Vargas committed
54
	unsigned long status;
55

Roberto Vargas's avatar
Roberto Vargas committed
56
	do {
57
58
		nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG);
		status = mmio_read_32(base_addr);
Roberto Vargas's avatar
Roberto Vargas committed
59
60
61
		if ((status & NOR_CMD_END) == NOR_CMD_END)
			return 0;
	} while (retries-- > 0);
62

Roberto Vargas's avatar
Roberto Vargas committed
63
	return -EBUSY;
64
65
}

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Return values:
 *    0      = success
 *    -EPERM = Device protected or Block locked
 *    -EIO   = General I/O error
 */
static int nor_full_status_check(uintptr_t base_addr)
{
	unsigned long status;

	/* Full status check */
	status = nor_status(base_addr);

	if (status & (NOR_PS | NOR_BLS | NOR_ESS | NOR_PSS))
		return -EPERM;
	if (status & (NOR_VPPS | NOR_ES))
		return -EIO;
	return 0;
}

86
87
88
89
90
91
void nor_send_cmd(uintptr_t base_addr, unsigned long cmd)
{
	mmio_write_32(base_addr, NOR_2X16(cmd));
}

/*
Roberto Vargas's avatar
Roberto Vargas committed
92
93
94
 * This function programs a word in the flash. Be aware that it only
 * can reset bits that were previously set. It cannot set bits that
 * were previously reset. The resulting bits = old_bits & new bits.
95
 * Return values:
96
97
 *  0 = success
 *  otherwise it returns a negative value
98
99
100
101
102
103
 */
int nor_word_program(uintptr_t base_addr, unsigned long data)
{
	uint32_t status;
	int ret;

104
105
	nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);

106
107
108
109
110
	/* Set the device in write word mode */
	nor_send_cmd(base_addr, NOR_CMD_WORD_PROGRAM);
	mmio_write_32(base_addr, data);

	ret = nor_poll_dws(base_addr, DWS_WORD_PROGRAM_RETRIES);
Roberto Vargas's avatar
Roberto Vargas committed
111
112
113
114
	if (ret == 0) {
		/* Full status check */
		nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG);
		status = mmio_read_32(base_addr);
115

Roberto Vargas's avatar
Roberto Vargas committed
116
117
118
119
		if (status & (NOR_PS | NOR_BLS)) {
			nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);
			ret = -EPERM;
		}
120
121
	}

122
123
	if (ret == 0)
		ret = nor_full_status_check(base_addr);
124
	nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY);
125

126
127
128
	return ret;
}

129
130
131
132
/*
 * Erase a full 256K block
 * Return values:
 *  0 = success
133
 *  otherwise it returns a negative value
134
135
136
137
138
139
140
141
142
143
144
 */
int nor_erase(uintptr_t base_addr)
{
	int ret;

	nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);

	nor_send_cmd(base_addr, NOR_CMD_BLOCK_ERASE);
	nor_send_cmd(base_addr, NOR_CMD_BLOCK_ERASE_ACK);

	ret = nor_poll_dws(base_addr, DWS_WORD_ERASE_RETRIES);
145
146
	if (ret == 0)
		ret = nor_full_status_check(base_addr);
147
148
149
150
151
	nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY);

	return ret;
}

Roberto Vargas's avatar
Roberto Vargas committed
152
153
/*
 * Lock a full 256 block
154
155
156
 * Return values:
 *  0 = success
 *  otherwise it returns a negative value
Roberto Vargas's avatar
Roberto Vargas committed
157
 */
158
int nor_lock(uintptr_t base_addr)
159
{
160
161
	int ret;

162
163
	nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);

164
	nor_send_cmd(base_addr, NOR_CMD_LOCK_UNLOCK);
165
	nor_send_cmd(base_addr, NOR_LOCK_BLOCK);
166
167

	ret = nor_poll_dws(base_addr, DWS_WORD_LOCK_RETRIES);
168
169
	if (ret == 0)
		ret = nor_full_status_check(base_addr);
170
	nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY);
171
172

	return ret;
173
174
}

Roberto Vargas's avatar
Roberto Vargas committed
175
176
/*
 * unlock a full 256 block
177
178
179
 * Return values:
 *  0 = success
 *  otherwise it returns a negative value
Roberto Vargas's avatar
Roberto Vargas committed
180
 */
181
int nor_unlock(uintptr_t base_addr)
182
{
183
184
	int ret;

185
186
	nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);

187
	nor_send_cmd(base_addr, NOR_CMD_LOCK_UNLOCK);
188
	nor_send_cmd(base_addr, NOR_UNLOCK_BLOCK);
189
190

	ret = nor_poll_dws(base_addr, DWS_WORD_LOCK_RETRIES);
191
192
	if (ret == 0)
		ret = nor_full_status_check(base_addr);
193
	nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY);
194
195

	return ret;
196
}