summaryrefslogtreecommitdiff
path: root/plat/arm/board/common/drivers/norflash/norflash.c
blob: cc63d758ea2f9bb9c7b39f9428afbc45e4281429 (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
/*
 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <errno.h>
#include <mmio.h>
#include <norflash.h>

/* Helper macros to access two flash banks in parallel */
#define NOR_2X16(d)			((d << 16) | (d & 0xffff))

/*
 * 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

/*
 * Poll Write State Machine. Return values:
 *    0      = WSM ready
 *    -EBUSY = WSM busy after the number of retries
 */
static int nor_poll_dws(uintptr_t base_addr, unsigned int retries)
{
	uint32_t status;
	int ret;

	for (;;) {
		nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG);
		status = mmio_read_32(base_addr);
		if ((status & NOR_DWS) &&
		    (status & (NOR_DWS << 16))) {
			ret = 0;
			break;
		}
		if (retries-- == 0) {
			ret = -EBUSY;
			break;
		}
	}

	return ret;
}

void nor_send_cmd(uintptr_t base_addr, unsigned long cmd)
{
	mmio_write_32(base_addr, NOR_2X16(cmd));
}

/*
 * Return values:
 *    0      = success
 *    -EBUSY = WSM not ready
 *    -EPERM = Device protected or Block locked
 */
int nor_word_program(uintptr_t base_addr, unsigned long data)
{
	uint32_t status;
	int ret;

	/* 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);
	if (ret != 0) {
		goto word_program_end;
	}

	/* Full status check */
	nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG);
	status = mmio_read_32(base_addr);

	if (status & (NOR_PS | NOR_BLS)) {
		nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG);
		ret = -EPERM;
	}

word_program_end:
	nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY);
	return ret;
}

void nor_lock(uintptr_t base_addr)
{
	nor_send_cmd(base_addr, NOR_CMD_LOCK_UNLOCK);
	mmio_write_32(base_addr, NOR_2X16(NOR_LOCK_BLOCK));
	nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY);
}

void nor_unlock(uintptr_t base_addr)
{
	nor_send_cmd(base_addr, NOR_CMD_LOCK_UNLOCK);
	mmio_write_32(base_addr, NOR_2X16(NOR_UNLOCK_BLOCK));
	nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY);
}