1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
4  */
5 
6 #include <common.h>
7 #include <asm/io.h>
8 #include "vct.h"
9 
ebi_read(u32 addr)10 static u32 ebi_read(u32 addr)
11 {
12 	addr &= ~0xFC000000;
13 
14 	reg_write(EBI_CPU_IO_ACCS(EBI_BASE), EXT_DEVICE_CHANNEL_2 | addr);
15 	ebi_wait();
16 
17 	return reg_read(EBI_IO_ACCS_DATA(EBI_BASE));
18 }
19 
ebi_write_u16(u32 addr,u32 data,int fetchIO)20 static int ebi_write_u16(u32 addr, u32 data, int fetchIO)
21 {
22 	u32 val = (data << 16);
23 
24 	addr &= ~0xFC000000;
25 
26 	ebi_wait();
27 
28 	reg_write(EBI_IO_ACCS_DATA(EBI_BASE), val);
29 	reg_write(EBI_CPU_IO_ACCS(EBI_BASE),
30 		  EXT_DEVICE_CHANNEL_2 | EBI_CPU_WRITE | addr);
31 	ebi_wait();
32 
33 	if (fetchIO) {
34 		u32 counter = 0;
35 		while (!(reg_read(EBI_SIG_LEVEL(EBI_BASE)) & EXT_CPU_IORDY_SL)) {
36 			if (counter++ > 0xFFFFFF)
37 				return 1;
38 		}
39 	}
40 
41 	return 0;
42 }
43 
ebi_read_u16(u32 addr)44 static u16 ebi_read_u16(u32 addr)
45 {
46 	return ((ebi_read(addr) >> 16) & 0xFFFF);
47 }
48 
ebi_read_u8(u32 addr)49 static u8 ebi_read_u8(u32 addr)
50 {
51 	u32 val = ebi_read(addr) >> 16;
52 
53 	if (addr & 0x1)
54 		return val & 0xff;
55 	else
56 		return (val >> 8) & 0xff;
57 }
58 
59 /*
60  * EBI initialization for NOR FLASH access
61  */
ebi_init_nor_flash(void)62 int ebi_init_nor_flash(void)
63 {
64 	reg_write(EBI_DEV1_CONFIG1(EBI_BASE), 0x83000);
65 
66 	reg_write(EBI_DEV2_CONFIG1(EBI_BASE), 0x400002);
67 	reg_write(EBI_DEV2_CONFIG2(EBI_BASE), 0x50);
68 
69 	reg_write(EBI_DEV2_TIM1_RD1(EBI_BASE), 0x409113);
70 	reg_write(EBI_DEV2_TIM1_RD2(EBI_BASE), 0xFF01000);
71 	reg_write(EBI_DEV2_TIM1_WR1(EBI_BASE), 0x04003113);
72 	reg_write(EBI_DEV2_TIM1_WR2(EBI_BASE), 0x3FC12011);
73 	reg_write(EBI_DEV2_TIM_EXT(EBI_BASE), 0xFFF00000);
74 
75 	return 0;
76 }
77 
78 /*
79  * Accessor functions replacing the "weak" functions in
80  * drivers/mtd/cfi_flash.c
81  */
flash_write8(u8 value,void * addr)82 void flash_write8(u8 value, void *addr)
83 {
84 	ebi_write_u16((u32)addr, value, 0);
85 }
86 
flash_write16(u16 value,void * addr)87 void flash_write16(u16 value, void *addr)
88 {
89 	ebi_write_u16((u32)addr, value, 0);
90 }
91 
flash_read8(void * addr)92 u8 flash_read8(void *addr)
93 {
94 	return ebi_read_u8((u32)addr);
95 }
96 
flash_read16(void * addr)97 u16 flash_read16(void *addr)
98 {
99 	return ebi_read_u16((u32)addr);
100 }
101 
flash_read32(void * addr)102 u32 flash_read32(void *addr)
103 {
104 	return ((u32)ebi_read_u16((u32)addr) << 16) |
105 		ebi_read_u16((u32)addr + 2);
106 }
107 
board_flash_read_memcpy(void * dest,const void * src,size_t count)108 void *board_flash_read_memcpy(void *dest, const void *src, size_t count)
109 {
110 	u16 *tmp = (u16 *)dest, *s = (u16 *)src;
111 	int i;
112 
113 	for (i = 0; i < count; i += 2)
114 		*tmp++ = flash_read16(s++);
115 
116 	return dest;
117 }
118