summaryrefslogtreecommitdiff
path: root/chip/max32660/flash_chip.c
blob: 747d7dcc580857e47221d24c7b4eca22fd79ec09 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* MAX32660 Flash Memory Module for Chrome EC */

#include "flash.h"
#include "switch.h"
#include "system.h"
#include "timer.h"
#include "util.h"
#include "watchdog.h"
#include "registers.h"
#include "common.h"
#include "icc_regs.h"
#include "flc_regs.h"

#define CPUTS(outstr) cputs(CC_SYSTEM, outstr)
#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ##args)

/***** Definitions *****/

/// Bit mask that can be used to find the starting address of a page in flash
#define MXC_FLASH_PAGE_MASK ~(MXC_FLASH_PAGE_SIZE - 1)

/// Calculate the address of a page in flash from the page number
#define MXC_FLASH_PAGE_ADDR(page)                                              \
	(MXC_FLASH_MEM_BASE + ((unsigned long)page * MXC_FLASH_PAGE_SIZE))

void flash_operation(void)
{
	volatile uint32_t *line_addr;
	volatile uint32_t __attribute__((unused)) line;

	// Clear the cache
	MXC_ICC->cache_ctrl ^= MXC_F_ICC_CACHE_CTRL_CACHE_EN;
	MXC_ICC->cache_ctrl ^= MXC_F_ICC_CACHE_CTRL_CACHE_EN;

	// Clear the line fill buffer
	line_addr = (uint32_t *)(MXC_FLASH_MEM_BASE);
	line = *line_addr;

	line_addr = (uint32_t *)(MXC_FLASH_MEM_BASE + MXC_FLASH_PAGE_SIZE);
	line = *line_addr;
}

static int flash_busy(void)
{
	return (MXC_FLC->cn &
		(MXC_F_FLC_CN_WR | MXC_F_FLC_CN_ME | MXC_F_FLC_CN_PGE));
}

static int flash_init_controller(void)
{
	// Set flash clock divider to generate a 1MHz clock from the APB clock
	MXC_FLC->clkdiv = SystemCoreClock / 1000000;

	/* Check if the flash controller is busy */
	if (flash_busy()) {
		return EC_ERROR_BUSY;
	}

	/* Clear stale errors */
	if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
		MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
	}

	/* Unlock flash */
	MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_UNLOCK) |
		      MXC_S_FLC_CN_UNLOCK_UNLOCKED;

	return EC_SUCCESS;
}

static int flash_device_page_erase(uint32_t address)
{
	int err;

	if ((err = flash_init_controller()) != EC_SUCCESS)
		return err;

	// Align address on page boundary
	address = address - (address % MXC_FLASH_PAGE_SIZE);

	/* Write paflash_init_controllerde */
	MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) |
		      MXC_S_FLC_CN_ERASE_CODE_ERASEPAGE;
	/* Issue page erase command */
	MXC_FLC->addr = address;
	MXC_FLC->cn |= MXC_F_FLC_CN_PGE;

	/* Wait until flash operation is complete */
	while (flash_busy())
		;

	/* Lock flash */
	MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;

	/* Check access violations */
	if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
		MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
		return EC_ERROR_UNKNOWN;
	}

	flash_operation();

	return EC_SUCCESS;
}

int crec_flash_physical_write(int offset, int size, const char *data)
{
	int err;
	uint32_t bytes_written;
	uint8_t current_data[4];

	if ((err = flash_init_controller()) != EC_SUCCESS)
		return err;

	// write in 32-bit units until we are 128-bit aligned
	MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
	MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;

	// Align the address and read/write if we have to
	if (offset & 0x3) {

		// Figure out how many bytes we have to write to round up the
		// address
		bytes_written = 4 - (offset & 0x3);

		// Save the data currently in the flash
		memcpy(current_data, (void *)(offset & (~0x3)), 4);

		// Modify current_data to insert the data from buffer
		memcpy(&current_data[4 - bytes_written], data, bytes_written);

		// Write the modified data
		MXC_FLC->addr = offset - (offset % 4);
		memcpy((void *)&MXC_FLC->data[0], &current_data, 4);
		MXC_FLC->cn |= MXC_F_FLC_CN_WR;

		/* Wait until flash operation is complete */
		while (flash_busy())
			;

		offset += bytes_written;
		size -= bytes_written;
		data += bytes_written;
	}

	while ((size >= 4) && ((offset & 0x1F) != 0)) {
		MXC_FLC->addr = offset;
		memcpy((void *)&MXC_FLC->data[0], data, 4);
		MXC_FLC->cn |= MXC_F_FLC_CN_WR;

		/* Wait until flash operation is complete */
		while (flash_busy())
			;

		offset += 4;
		size -= 4;
		data += 4;
	}

	if (size >= 16) {

		// write in 128-bit bursts while we can
		MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;

		while (size >= 16) {
			MXC_FLC->addr = offset;
			memcpy((void *)&MXC_FLC->data[0], data, 16);
			MXC_FLC->cn |= MXC_F_FLC_CN_WR;

			/* Wait until flash operation is complete */
			while (flash_busy())
				;

			offset += 16;
			size -= 16;
			data += 16;
		}

		// Return to 32-bit writes.
		MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
	}

	while (size >= 4) {
		MXC_FLC->addr = offset;
		memcpy((void *)&MXC_FLC->data[0], data, 4);
		MXC_FLC->cn |= MXC_F_FLC_CN_WR;

		/* Wait until flash operation is complete */
		while (flash_busy())
			;

		offset += 4;
		size -= 4;
		data += 4;
	}

	if (size > 0) {
		// Save the data currently in the flash
		memcpy(current_data, (void *)(offset), 4);

		// Modify current_data to insert the data from data
		memcpy(current_data, data, size);

		MXC_FLC->addr = offset;
		memcpy((void *)&MXC_FLC->data[0], current_data, 4);
		MXC_FLC->cn |= MXC_F_FLC_CN_WR;

		/* Wait until flash operation is complete */
		while (flash_busy())
			;
	}

	/* Lock flash */
	MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;

	/* Check access violations */
	if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
		MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
		return EC_ERROR_UNKNOWN;
	}

	flash_operation();

	return EC_SUCCESS;
}

/*****************************************************************************/
/* Physical layer APIs */

int crec_flash_physical_erase(int offset, int size)
{
	int i;
	int pages;
	int error_status;

	/*
	 * erase 'size' number of bytes starting at address 'offset'
	 */
	/* calculate the number of pages */
	pages = size / CONFIG_FLASH_ERASE_SIZE;
	/* iterate over the number of pages */
	for (i = 0; i < pages; i++) {
		/* erase the page after calculating the start address */
		error_status = flash_device_page_erase(
			offset + (i * CONFIG_FLASH_ERASE_SIZE));
		if (error_status != EC_SUCCESS) {
			return error_status;
		}
	}
	return EC_SUCCESS;
}

int crec_flash_physical_get_protect(int bank)
{
	/* Not protected */
	return 0;
}

uint32_t crec_flash_physical_get_protect_flags(void)
{
	/* no flags set */
	return 0;
}

uint32_t crec_flash_physical_get_valid_flags(void)
{
	/* These are the flags we're going to pay attention to */
	return EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW |
	       EC_FLASH_PROTECT_ALL_NOW;
}

uint32_t crec_flash_physical_get_writable_flags(uint32_t cur_flags)
{
	/* no flags writable */
	return 0;
}

int crec_flash_physical_protect_at_boot(uint32_t new_flags)
{
	/* nothing to do here */
	return EC_SUCCESS;
}

int crec_flash_physical_protect_now(int all)
{
	/* nothing to do here */
	return EC_SUCCESS;
}

/*****************************************************************************/
/* High-level APIs */

int crec_flash_pre_init(void)
{
	return EC_SUCCESS;
}

/*****************************************************************************/
/* Test Commands */

/*
 * Read, Write, and Erase a page of flash memory using chip routines
 * NOTE: This is a DESTRUCTIVE test for the range of flash pages tested
 *       make sure that PAGE_START is beyond your flash code.
 */
static int command_flash_test1(int argc, char **argv)
{
	int i;
	uint8_t *ptr;
	const uint32_t PAGE_START = 9;
	const uint32_t PAGE_END = 32;
	uint32_t page;
	int error_status;
	uint32_t flash_address;
	const int BUFFER_SIZE = 32;
	uint8_t buffer[BUFFER_SIZE];

	/*
	 * As a test, write unique data to each page in this for loop, later
	 * verify data in pages
	 */
	for (page = PAGE_START; page < PAGE_END; page++) {
		flash_address = page * CONFIG_FLASH_ERASE_SIZE;

		/*
		 * erase page
		 */
		error_status = crec_flash_physical_erase(flash_address,
						    CONFIG_FLASH_ERASE_SIZE);
		if (error_status != EC_SUCCESS) {
			CPRINTS("Error with crec_flash_physical_erase\n");
			return EC_ERROR_UNKNOWN;
		}

		/*
		 * verify page was erased
		 */
		// CPRINTS("read flash page %d, address %x, ", page,
		// flash_address);
		ptr = (uint8_t *)flash_address;
		for (i = 0; i < CONFIG_FLASH_ERASE_SIZE; i++) {
			if (*ptr++ != 0xff) {
				CPRINTS("Error with verifying page erase\n");
				return EC_ERROR_UNKNOWN;
			}
		}

		/*
		 * write pattern to page, just write BUFFER_SIZE worth of data
		 */
		for (i = 0; i < BUFFER_SIZE; i++) {
			buffer[i] = i + page;
		}
		error_status = crec_flash_physical_write(flash_address,
							 BUFFER_SIZE, buffer);
		if (error_status != EC_SUCCESS) {
			CPRINTS("Error with crec_flash_physical_write\n");
			return EC_ERROR_UNKNOWN;
		}
	}

	/*
	 * Verify data in pages
	 */
	for (page = PAGE_START; page < PAGE_END; page++) {
		flash_address = page * CONFIG_FLASH_ERASE_SIZE;

		/*
		 * read a portion of flash memory
		 */
		ptr = (uint8_t *)flash_address;
		for (i = 0; i < BUFFER_SIZE; i++) {
			if (*ptr++ != (i + page)) {
				CPRINTS("Error with verifing written test "
					"data\n");
				return EC_ERROR_UNKNOWN;
			}
		}
		CPRINTS("Verified Erase, Write, Read page %d", page);
	}

	/*
	 * Clean up after tests
	 */
	for (page = PAGE_START; page <= PAGE_END; page++) {
		flash_address = page * CONFIG_FLASH_ERASE_SIZE;
		error_status = crec_flash_physical_erase(flash_address,
						    CONFIG_FLASH_ERASE_SIZE);
		if (error_status != EC_SUCCESS) {
			CPRINTS("Error with crec_flash_physical_erase\n");
			return EC_ERROR_UNKNOWN;
		}
	}

	CPRINTS("done command_flash_test1.");
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(flashtest1, command_flash_test1, "flashtest1",
			"Flash chip routine tests");