aboutsummaryrefslogtreecommitdiffstats
path: root/src/cc32/cc32_flcon.c
blob: fc9ff1cbc54ff64309d1552b658a87e4c7c5e52c (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
/*
 * ChipCity CC32RS512 Flash controller driver
 *
 * Copyright (C) 2012 Harald Welte <laforge@gnumonks.org>
 *
 * 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; either version 2 or
 * (at your option) version 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include <errno.h>

#include "cc32_irq.h"
#include "cc32_flcon.h"

#define CC32_FLCON_BASE	0x0f2000

enum cc32_flcon_reg {
	FLCON		= 0x00,
	FLSDP1		= 0x04,
	FLSDP2		= 0x08,
	FLSTS		= 0x0C,
	FLBUF		= 0x10,
};

#define FLCON_REG(x)	(uint32_t *)((uint8_t *)CC32_FLCON_BASE + x)


int cc32_flash_erase(uint32_t offset, uint16_t page_size)
{
	switch (page_size) {
	case 256:
		*FLCON_REG(FLCON) = (1 << 3);
		break;
	case 512:
		*FLCON_REG(FLCON) = (0 << 3);
		break;
	default:
		return -EINVAL;
	}

	/* if we program/erase the first page, we have to disable the interrupt */
	if (offset < page_size)
		cc32_irq_disable(IRQ_FLCON);

	/* clear all flags */
	*FLCON_REG(FLSTS) = 3;

	*FLCON_REG(FLSDP1) = 0x55;
	*FLCON_REG(FLSDP2) = 0xAA;

	*(uint32_t *)offset = 0xFF;

	while (!(*FLCON_REG(FLSTS) & 1)) {}

	return 0;
}

int cc32_flash_program(uint32_t offset, uint16_t page_size, uint32_t *buffer)
{
	switch (page_size) {
	case 256:
		*FLCON_REG(FLCON) = (1 << 3);
		break;
	case 512:
		*FLCON_REG(FLCON) = (0 << 3);
		break;
	default:
		return -EINVAL;
	}

	/* if we program/erase the first page, we have to disable the interrupt */
	if (offset < page_size)
		cc32_irq_disable(IRQ_FLCON);

	/* clear all flags */
	*FLCON_REG(FLSTS) = 3;

	*FLCON_REG(FLBUF) = offset;
	*FLCON_REG(FLSDP1) = 0xAA;
	*FLCON_REG(FLSDP2) = 0x55;

	*(uint32_t *)offset = 0x00;

	while (!(*FLCON_REG(FLSTS) & 1)) {}

	/* clear flag */
	*FLCON_REG(FLSTS) |= 1;

	return 0;
}