blob: a9d0ed8d14af27206ed1bd8c4726bb5289ef7e65 [file] [log] [blame]
Stefan Roese8a316c92005-08-01 16:49:12 +02001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese8a316c92005-08-01 16:49:12 +02006 */
7
8/*
9 * Modified 4/5/2001
10 * Wait for completion of each sector erase command issued
11 * 4/5/2001
12 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
13 */
14
15#include <common.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020016#include <asm/ppc4xx.h>
Stefan Roese8a316c92005-08-01 16:49:12 +020017#include <asm/processor.h>
18
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020019flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
Stefan Roese8a316c92005-08-01 16:49:12 +020020
21#undef DEBUG
22#ifdef DEBUG
23#define DEBUGF(x...) printf(x)
24#else
25#define DEBUGF(x...)
26#endif /* DEBUG */
27
28/*
29 * include common flash code (for amcc boards)
30 */
31#include "../common/flash.c"
32
33/*-----------------------------------------------------------------------
34 * Functions
35 */
36static ulong flash_get_size(vu_long * addr, flash_info_t * info);
37static void flash_get_offsets(ulong base, flash_info_t * info);
38
39unsigned long flash_init(void)
40{
41 unsigned long size_b0, size_b1;
42 int i;
43 uint pbcr;
44 unsigned long base_b0, base_b1;
45
46 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Stefan Roese8a316c92005-08-01 16:49:12 +020048 flash_info[i].flash_id = FLASH_UNKNOWN;
49 }
50
51 /* Static FLASH Bank configuration here - FIXME XXX */
52
53 size_b0 =
54 flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
55
56 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
57 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
58 size_b0, size_b0 << 20);
59 }
60
61 /* Only one bank */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062 if (CONFIG_SYS_MAX_FLASH_BANKS == 1) {
Stefan Roese8a316c92005-08-01 16:49:12 +020063 /* Setup offsets */
64 flash_get_offsets(FLASH_BASE0_PRELIM, &flash_info[0]);
65
66 /* Monitor protection ON by default */
67 (void)flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068 CONFIG_SYS_MONITOR_BASE,
69 CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
Stefan Roese8a316c92005-08-01 16:49:12 +020070 &flash_info[0]);
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020071#ifdef CONFIG_ENV_IS_IN_FLASH
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020072 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
73 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
Stefan Roese8a316c92005-08-01 16:49:12 +020074 &flash_info[0]);
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020075 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR_REDUND,
76 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
Stefan Roese8a316c92005-08-01 16:49:12 +020077 &flash_info[0]);
78#endif
79
80 size_b1 = 0;
81 flash_info[0].size = size_b0;
82 }
83
84 /* 2 banks */
85 else {
86 size_b1 =
87 flash_get_size((vu_long *) FLASH_BASE1_PRELIM,
88 &flash_info[1]);
89
90 /* Re-do sizing to get full correct info */
91
92 if (size_b1) {
Stefan Roesed1c3b272009-09-09 16:25:29 +020093 mtdcr(EBC0_CFGADDR, PB0CR);
94 pbcr = mfdcr(EBC0_CFGDATA);
95 mtdcr(EBC0_CFGADDR, PB0CR);
Stefan Roese8a316c92005-08-01 16:49:12 +020096 base_b1 = -size_b1;
97 pbcr = (pbcr & 0x0001ffff) | base_b1 |
98 (((size_b1 / 1024 / 1024) - 1) << 17);
Stefan Roesed1c3b272009-09-09 16:25:29 +020099 mtdcr(EBC0_CFGDATA, pbcr);
100 /* printf("PB1CR = %x\n", pbcr); */
Stefan Roese8a316c92005-08-01 16:49:12 +0200101 }
102
103 if (size_b0) {
Stefan Roesed1c3b272009-09-09 16:25:29 +0200104 mtdcr(EBC0_CFGADDR, PB1CR);
105 pbcr = mfdcr(EBC0_CFGDATA);
106 mtdcr(EBC0_CFGADDR, PB1CR);
Stefan Roese8a316c92005-08-01 16:49:12 +0200107 base_b0 = base_b1 - size_b0;
108 pbcr = (pbcr & 0x0001ffff) | base_b0 |
109 (((size_b0 / 1024 / 1024) - 1) << 17);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200110 mtdcr(EBC0_CFGDATA, pbcr);
111 /* printf("PB0CR = %x\n", pbcr); */
Stefan Roese8a316c92005-08-01 16:49:12 +0200112 }
113
114 size_b0 = flash_get_size((vu_long *) base_b0, &flash_info[0]);
115
116 flash_get_offsets(base_b0, &flash_info[0]);
117
118 /* monitor protection ON by default */
119 (void)flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200120 base_b0 + size_b0 - CONFIG_SYS_MONITOR_LEN,
Stefan Roese8a316c92005-08-01 16:49:12 +0200121 base_b0 + size_b0 - 1, &flash_info[0]);
122 /* Also protect sector containing initial power-up instruction */
123 /* (flash_protect() checks address range - other call ignored) */
124 (void)flash_protect(FLAG_PROTECT_SET,
125 0xFFFFFFFC, 0xFFFFFFFF, &flash_info[0]);
126 (void)flash_protect(FLAG_PROTECT_SET,
127 0xFFFFFFFC, 0xFFFFFFFF, &flash_info[1]);
128
129 if (size_b1) {
130 /* Re-do sizing to get full correct info */
131 size_b1 =
132 flash_get_size((vu_long *) base_b1, &flash_info[1]);
133
134 flash_get_offsets(base_b1, &flash_info[1]);
135
136 /* monitor protection ON by default */
137 (void)flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200138 base_b1 + size_b1 - CONFIG_SYS_MONITOR_LEN,
Stefan Roese8a316c92005-08-01 16:49:12 +0200139 base_b1 + size_b1 - 1,
140 &flash_info[1]);
141 /* monitor protection OFF by default (one is enough) */
142 (void)flash_protect(FLAG_PROTECT_CLEAR,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200143 base_b0 + size_b0 - CONFIG_SYS_MONITOR_LEN,
Stefan Roese8a316c92005-08-01 16:49:12 +0200144 base_b0 + size_b0 - 1,
145 &flash_info[0]);
146 } else {
147 flash_info[1].flash_id = FLASH_UNKNOWN;
148 flash_info[1].sector_count = -1;
149 }
150
151 flash_info[0].size = size_b0;
152 flash_info[1].size = size_b1;
153 } /* else 2 banks */
154 return (size_b0 + size_b1);
155}
156
157static void flash_get_offsets(ulong base, flash_info_t * info)
158{
159 int i;
160
161 /* set up sector start address table */
162 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
163 (info->flash_id == FLASH_AM040)) {
164 for (i = 0; i < info->sector_count; i++)
165 info->start[i] = base + (i * 0x00010000);
166 } else {
167 if (info->flash_id & FLASH_BTYPE) {
168 /* set sector offsets for bottom boot block type */
169 info->start[0] = base + 0x00000000;
170 info->start[1] = base + 0x00004000;
171 info->start[2] = base + 0x00006000;
172 info->start[3] = base + 0x00008000;
173 for (i = 4; i < info->sector_count; i++) {
174 info->start[i] =
175 base + (i * 0x00010000) - 0x00030000;
176 }
177 } else {
178 /* set sector offsets for top boot block type */
179 i = info->sector_count - 1;
180 info->start[i--] = base + info->size - 0x00004000;
181 info->start[i--] = base + info->size - 0x00006000;
182 info->start[i--] = base + info->size - 0x00008000;
183 for (; i >= 0; i--) {
184 info->start[i] = base + i * 0x00010000;
185 }
186 }
187 }
188}