1 /*
2  * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307 USA
18  */
19 
20 #include <common.h>
21 #include <asm/io.h>
22 #include "vct.h"
23 
24 static u32 ebi_read(u32 addr)
25 {
26 	addr &= ~0xFC000000;
27 
28 	reg_write(EBI_CPU_IO_ACCS(EBI_BASE), EXT_DEVICE_CHANNEL_2 | addr);
29 	ebi_wait();
30 
31 	return reg_read(EBI_IO_ACCS_DATA(EBI_BASE));
32 }
33 
34 static int ebi_write_u16(u32 addr, u32 data, int fetchIO)
35 {
36 	u32 val = (data << 16);
37 
38 	addr &= ~0xFC000000;
39 
40 	ebi_wait();
41 
42 	reg_write(EBI_IO_ACCS_DATA(EBI_BASE), val);
43 	reg_write(EBI_CPU_IO_ACCS(EBI_BASE),
44 		  EXT_DEVICE_CHANNEL_2 | EBI_CPU_WRITE | addr);
45 	ebi_wait();
46 
47 	if (fetchIO) {
48 		u32 counter = 0;
49 		while (!(reg_read(EBI_SIG_LEVEL(EBI_BASE)) & EXT_CPU_IORDY_SL)) {
50 			if (counter++ > 0xFFFFFF)
51 				return 1;
52 		}
53 	}
54 
55 	return 0;
56 }
57 
58 static u16 ebi_read_u16(u32 addr)
59 {
60 	return ((ebi_read(addr) >> 16) & 0xFFFF);
61 }
62 
63 static u8 ebi_read_u8(u32 addr)
64 {
65 	u32 val = ebi_read(addr) >> 16;
66 
67 	if (addr & 0x1)
68 		return val & 0xff;
69 	else
70 		return (val >> 8) & 0xff;
71 }
72 
73 /*
74  * EBI initialization for NOR FLASH access
75  */
76 int ebi_init_nor_flash(void)
77 {
78 	reg_write(EBI_DEV1_CONFIG1(EBI_BASE), 0x83000);
79 
80 	reg_write(EBI_DEV2_CONFIG1(EBI_BASE), 0x400002);
81 	reg_write(EBI_DEV2_CONFIG2(EBI_BASE), 0x50);
82 
83 	reg_write(EBI_DEV2_TIM1_RD1(EBI_BASE), 0x409113);
84 	reg_write(EBI_DEV2_TIM1_RD2(EBI_BASE), 0xFF01000);
85 	reg_write(EBI_DEV2_TIM1_WR1(EBI_BASE), 0x04003113);
86 	reg_write(EBI_DEV2_TIM1_WR2(EBI_BASE), 0x3FC12011);
87 	reg_write(EBI_DEV2_TIM_EXT(EBI_BASE), 0xFFF00000);
88 
89 	return 0;
90 }
91 
92 /*
93  * Accessor functions replacing the "weak" functions in
94  * drivers/mtd/cfi_flash.c
95  */
96 void flash_write8(u8 value, void *addr)
97 {
98 	ebi_write_u16((u32)addr, value, 0);
99 }
100 
101 void flash_write16(u16 value, void *addr)
102 {
103 	ebi_write_u16((u32)addr, value, 0);
104 }
105 
106 u8 flash_read8(void *addr)
107 {
108 	return ebi_read_u8((u32)addr);
109 }
110 
111 u16 flash_read16(void *addr)
112 {
113 	return ebi_read_u16((u32)addr);
114 }
115 
116 u32 flash_read32(void *addr)
117 {
118 	return ((u32)ebi_read_u16((u32)addr) << 16) |
119 		ebi_read_u16((u32)addr + 2);
120 }
121 
122 void *board_flash_read_memcpy(void *dest, const void *src, size_t count)
123 {
124 	u16 *tmp = (u16 *)dest, *s = (u16 *)src;
125 	int i;
126 
127 	for (i = 0; i < count; i += 2)
128 		*tmp++ = flash_read16(s++);
129 
130 	return dest;
131 }
132