summaryrefslogtreecommitdiff
path: root/board/micronas/vct/ebi_nor_flash.c
blob: 80fbc847b40909c4f874a89197067c9cdf2488b3 (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
/*
 * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <asm/io.h>
#include "vct.h"

static u32 ebi_read(u32 addr)
{
	addr &= ~0xFC000000;

	reg_write(EBI_CPU_IO_ACCS(EBI_BASE), EXT_DEVICE_CHANNEL_2 | addr);
	ebi_wait();

	return reg_read(EBI_IO_ACCS_DATA(EBI_BASE));
}

static int ebi_write_u16(u32 addr, u32 data, int fetchIO)
{
	u32 val = (data << 16);

	addr &= ~0xFC000000;

	ebi_wait();

	reg_write(EBI_IO_ACCS_DATA(EBI_BASE), val);
	reg_write(EBI_CPU_IO_ACCS(EBI_BASE),
		  EXT_DEVICE_CHANNEL_2 | EBI_CPU_WRITE | addr);
	ebi_wait();

	if (fetchIO) {
		u32 counter = 0;
		while (!(reg_read(EBI_SIG_LEVEL(EBI_BASE)) & EXT_CPU_IORDY_SL)) {
			if (counter++ > 0xFFFFFF)
				return 1;
		}
	}

	return 0;
}

static u16 ebi_read_u16(u32 addr)
{
	return ((ebi_read(addr) >> 16) & 0xFFFF);
}

static u8 ebi_read_u8(u32 addr)
{
	u32 val = ebi_read(addr) >> 16;

	if (addr & 0x1)
		return val & 0xff;
	else
		return (val >> 8) & 0xff;
}

/*
 * EBI initialization for NOR FLASH access
 */
int ebi_init_nor_flash(void)
{
	reg_write(EBI_DEV1_CONFIG1(EBI_BASE), 0x83000);

	reg_write(EBI_DEV2_CONFIG1(EBI_BASE), 0x400002);
	reg_write(EBI_DEV2_CONFIG2(EBI_BASE), 0x50);

	reg_write(EBI_DEV2_TIM1_RD1(EBI_BASE), 0x409113);
	reg_write(EBI_DEV2_TIM1_RD2(EBI_BASE), 0xFF01000);
	reg_write(EBI_DEV2_TIM1_WR1(EBI_BASE), 0x04003113);
	reg_write(EBI_DEV2_TIM1_WR2(EBI_BASE), 0x3FC12011);
	reg_write(EBI_DEV2_TIM_EXT(EBI_BASE), 0xFFF00000);

	return 0;
}

/*
 * Accessor functions replacing the "weak" functions in
 * drivers/mtd/cfi_flash.c
 */
void flash_write8(u8 value, void *addr)
{
	ebi_write_u16((u32)addr, value, 0);
}

void flash_write16(u16 value, void *addr)
{
	ebi_write_u16((u32)addr, value, 0);
}

u8 flash_read8(void *addr)
{
	return ebi_read_u8((u32)addr);
}

u16 flash_read16(void *addr)
{
	return ebi_read_u16((u32)addr);
}

u32 flash_read32(void *addr)
{
	return ((u32)ebi_read_u16((u32)addr) << 16) |
		ebi_read_u16((u32)addr + 2);
}

void *board_flash_read_memcpy(void *dest, const void *src, size_t count)
{
	u16 *tmp = (u16 *)dest, *s = (u16 *)src;
	int i;

	for (i = 0; i < count; i += 2)
		*tmp++ = flash_read16(s++);

	return dest;
}