summaryrefslogtreecommitdiff
path: root/board/wmt/flash.c
blob: c0270042e2142d80f79a34e610310a6f07880670 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*++ 
Copyright (c) 2013 WonderMedia Technologies, Inc.

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 of the License, or (at your option) any later version.

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

WonderMedia Technologies, Inc.
10F, 529, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C.
--*/
/*
 * (C) Copyright 2000
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * Some descriptions of such software. Copyright (c) 2008 WonderMedia Technologies, Inc.
 *
 * 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 of the License, or (at your option) any later version.
 *
 * 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/>.
 *
 * WonderMedia Technologies, Inc.
 * 10F, 529, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C.
 */

#include <common.h>
#include <mpc8xx.h>
#include "flash/spi_flash.h"
#include "flash/nor_flash_8bit.h"
#include "flash/nor_flash_16bit.h"
/* #include "flash/nor_flash.h" */
#include "flash/nand_flash.h"
#include "include/extvars.h"

typedef enum _BOOT_FLASH_TYPE {
	NOR_FLASH_TYPE = 0,
	ROM_FLASH_TYPE,
	NAND_FLASH_TYPE,
	SPI_FLASH_TYPE,
	MMC_FLASH_TYPE
} BOOT_FLASH_TYPE;

typedef enum _BOOT_BIT_TYPE {
	BOOT_TYPE_8BIT = 0,
	BOOT_TYPE_16BIT
} BOOT_BIT_TYPE;

BOOT_FLASH_TYPE flash_type = SPI_FLASH_TYPE;
BOOT_BIT_TYPE  nor_flash_bit = BOOT_TYPE_16BIT;
BOOT_BIT_TYPE  nand_flash_bit = BOOT_TYPE_16BIT;

flash_info_t flash_info_nor[CFG_MAX_NOR_FLASH_BANKS];
flash_info_t flash_info_spi[CFG_MAX_SPI_FLASH_BANKS];
flash_info_t flash_info_nand[CFG_MAX_NAND_FLASH_BANKS];

flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
static unsigned int flash_count;
/*
 * get_boot_flash_type()
 */
BOOT_FLASH_TYPE get_boot_flash_type(void)
{
	unsigned int val = 0;
	unsigned char rc = SPI_FLASH_TYPE;

	val = *((volatile unsigned int *)(0xd8110100));
	if ((val&(1<<14)) == 0)
		rc = MMC_FLASH_TYPE;
	else if (val&8)
		rc = ROM_FLASH_TYPE;
	else
		rc = SPI_FLASH_TYPE;

	return rc;
}

/*
 * get_boot_nand_flash_bit_type()
 */
BOOT_BIT_TYPE get_boot_nand_flash_bit_type(void)
{
	unsigned int val = 0;
	unsigned char rc = BOOT_TYPE_8BIT;

	val = *((volatile unsigned int *)(0xd8110100));

	val = (val >> 4) & 0x1;

	switch (val) {
	case 0:
		rc = BOOT_TYPE_8BIT;
		break;
	case 1:
		rc = BOOT_TYPE_16BIT;
		break;
	default:
		break;
	}

	return rc;
}

static void print_flash_addr(flash_info_t *info)
{
	flash_info_t *p = NULL;
	int i;

	if (!flash_count)
		return;
	printf("flash:\n");
	for (i = 0, p = info; flash_count != 0; p++, i++, flash_count--) {
		printf("     Bank%d: %08lX -- %08lX", i+1, p->start[0], p->start[0]+p->size-1);

		if (p->flash_id == FLASH_UNKNOWN)
			printf(" (Missing or Unknown Flash)");
		printf("\n");
	}
}

/*
 * flash_init()
 *
 * sets up flash_info and returns size of FLASH (bytes)
 */
unsigned long flash_init(void)
{
	unsigned long rc = 0;
	int i;

	flash_type = get_boot_flash_type();
	nand_flash_bit = get_boot_nand_flash_bit_type();
	flash_count = 0;
	/* because FLASH_UNKNOWN = 0XFFFffffF, SO WE USE FF INITIAL */
	memset(flash_info, 0xff, CFG_MAX_FLASH_BANKS * sizeof(flash_info_t));
	switch (flash_type) {
	/*case NOR_FLASH_TYPE:
		printf("boot from nor flash: ");
		flash_count = CFG_MAX_NOR_FLASH_BANKS;
		switch (nor_flash_bit) {
		case BOOT_TYPE_8BIT:
			printf("8bit mode.\n");
			rc = nor_flash_8b_init();
			break;
		case BOOT_TYPE_16BIT:
			printf("16bit mode.\n");
			rc = nor_flash_16b_init();
			break;
		}
		// rc = nor_flash_init();
		for (i = 0; i < CFG_MAX_NOR_FLASH_BANKS; i++)
			memcpy(&flash_info[i], &flash_info_nor[i], sizeof(flash_info_t));
		break;*/
	case NAND_FLASH_TYPE:
		printf("boot from nand flash.\n");
		flash_count = CFG_MAX_NAND_FLASH_BANKS;
		rc = nand_flash_init();
		for (i = 0; i < CFG_MAX_NAND_FLASH_BANKS; i++)
			memcpy(&flash_info[i], &flash_info_nand[i], sizeof(flash_info_t));
		break;
	case SPI_FLASH_TYPE:
		printf("boot from spi flash.\n");
		flash_count = CFG_MAX_SPI_FLASH_BANKS;
		rc = spi_flash_init();
		for (i = 0; i < CFG_MAX_SPI_FLASH_BANKS; i++)
			memcpy(&flash_info[i], &flash_info_spi[i], sizeof(flash_info_t));
		break;
	case ROM_FLASH_TYPE:
		printf("boot from ROM.\n");
	default:
		printf("boot from unknow device====================\n");
		break;
	}
	print_flash_addr(flash_info);

	return rc;
}

/*
 */
void flash_print_info(flash_info_t *info)
{
	switch (flash_type) {
#if 0
	case NOR_FLASH_TYPE:
		switch (nor_flash_bit) {
		case BOOT_TYPE_8BIT:
			nor_flash_8b_print_info(info);
			break;
		case BOOT_TYPE_16BIT:
			nor_flash_16b_print_info(info);
			break;
		}

		/* nor_flash_print_info(info); */
		break;
#endif
	case NAND_FLASH_TYPE:
		nand_flash_print_info(info);
		break;
	case SPI_FLASH_TYPE:
		spi_flash_print_info(info);
		break;
	default:
		break;
	}
}

/*
 * flash_erase
 */
int	flash_erase(flash_info_t *info, int s_first, int s_last)
{
	int rc = 0;

	switch (flash_type) {
#if 0
	case NOR_FLASH_TYPE:
		switch (nor_flash_bit) {
		case BOOT_TYPE_8BIT:
			rc = nor_flash_8b_erase(info, s_first, s_last);
			break;
		case BOOT_TYPE_16BIT:
			rc = nor_flash_16b_erase(info, s_first, s_last);
			break;
		}

		/* rc = nor_flash_erase(info, s_first, s_last); */
		break;
#endif
	case NAND_FLASH_TYPE:
		rc = nand_flash_erase(info, s_first, s_last);
		break;
	case SPI_FLASH_TYPE:
    /* sf boot, nand active */
		rc = spi_flash_erase(info, s_first, s_last);
		break;
	default:
		break;
	}

	return rc;
}

/*
 * Copy flash to memory, returns:
 * 0 - OK
 * 1 - write timeout
 */
int read_buff(flash_info_t *info, ulong src, ulong addr, ulong cnt)
{
	int rc = 0;

	rc = spi_read_buff(info, src, addr, cnt);

	return rc;
}

/*
 * Copy memory to flash, returns:
 * 0 - OK
 * 1 - write timeout
 * 2 - Flash not erased
 */
int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
{
	int rc = 0;

	switch (flash_type) {
#if 0
	case NOR_FLASH_TYPE:
		switch (nor_flash_bit) {
		case BOOT_TYPE_8BIT:
			rc = nor_flash_8b_write_buff(info, src, addr, cnt);
			break;
		case BOOT_TYPE_16BIT:
			rc = nor_flash_16b_write_buff(info, src, addr, cnt);
			break;
		}

		/* rc = nor_write_buff(info, src, addr, cnt); */
		break;
#endif
	case NAND_FLASH_TYPE:
		rc = nand_write_buff(info, src, addr, cnt);
		break;
	case SPI_FLASH_TYPE:
    /* sf boot, nand active */
		rc = spi_write_buff(info, src, addr, cnt);
		break;
	default:
		break;
	}

	return rc;
}

int flash_real_protect(flash_info_t *info, long sector, int prot)
{
	int rc = 0;

	switch (flash_type) {
	case NOR_FLASH_TYPE:
		/* rc = nor_flash_real_protect(info, info->start[sector], prot); */
		break;
	case NAND_FLASH_TYPE:
		/*rc = nand_flash_real_protect(info, info->start[sector], prot);*/
		break;
	case SPI_FLASH_TYPE:
    /* sf boot, nand active */
		rc = spi_flash_real_protect(info, sector, prot);
		break;
	default:
		break;
	}

	return rc;
} /* end flash_real_protect() */