v2m_flash.c 4.53 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
8
 */

#include <errno.h>
#include <mmio.h>
9
#include <v2m_flash.h>
10

11
12
13
14
15
16
17
/*
 * 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.
 */
18
19
20
21
22
23
24

/*
 * 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
25
#define DWS_WORD_ERASE_RETRIES		3000000
26
#define DWS_WORD_LOCK_RETRIES		1000
27

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

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* 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:
48
49
50
 *    0      = WSM ready
 *    -EBUSY = WSM busy after the number of retries
 */
51
static int nor_poll_dws(uintptr_t base_addr, unsigned long int retries)
52
{
Roberto Vargas's avatar
Roberto Vargas committed
53
	unsigned long status;
54

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

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

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * 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;
}

85
86
87
88
89
90
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
91
92
93
 * 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.
94
 * Return values:
95
96
 *  0 = success
 *  otherwise it returns a negative value
97
98
99
100
101
102
 */
int nor_word_program(uintptr_t base_addr, unsigned long data)
{
	uint32_t status;
	int ret;

103
104
	nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);

105
106
107
108
109
	/* 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
110
111
112
113
	if (ret == 0) {
		/* Full status check */
		nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG);
		status = mmio_read_32(base_addr);
114

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

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

125
126
127
	return ret;
}

128
129
130
131
/*
 * Erase a full 256K block
 * Return values:
 *  0 = success
132
 *  otherwise it returns a negative value
133
134
135
136
137
138
139
140
141
142
143
 */
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);
144
145
	if (ret == 0)
		ret = nor_full_status_check(base_addr);
146
147
148
149
150
	nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY);

	return ret;
}

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

161
162
	nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);

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

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

	return ret;
172
173
}

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

184
185
	nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);

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

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

	return ret;
195
}