blob: 5388872074ba205310ac523f8390a888c148337b [file] [log] [blame]
wdenkc7de8292002-11-19 11:04:11 +00001/*
Marian Balakowicz66080432006-09-01 19:46:22 +02002 * (C) Copyright 2000-2006
wdenkc7de8292002-11-19 11:04:11 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc7de8292002-11-19 11:04:11 +00006 */
7
8#include <common.h>
9#include <flash.h>
10
Marian Balakowicze6f2e902005-10-11 19:09:42 +020011extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenkc7de8292002-11-19 11:04:11 +000012
13/*-----------------------------------------------------------------------
14 * Functions
15 */
16
17/*-----------------------------------------------------------------------
18 * Set protection status for monitor sectors
19 *
20 * The monitor is always located in the _first_ Flash bank.
21 * If necessary you have to map the second bank at lower addresses.
22 */
23void
24flash_protect (int flag, ulong from, ulong to, flash_info_t *info)
25{
26 ulong b_end = info->start[0] + info->size - 1; /* bank end address */
27 short s_end = info->sector_count - 1; /* index of last sector */
28 int i;
29
30 /* Do nothing if input data is bad. */
31 if (info->sector_count == 0 || info->size == 0 || to < from) {
32 return;
33 }
34
35 /* There is nothing to do if we have no data about the flash
36 * or the protect range and flash range don't overlap.
37 */
38 if (info->flash_id == FLASH_UNKNOWN ||
39 to < info->start[0] || from > b_end) {
40 return;
41 }
42
43 for (i=0; i<info->sector_count; ++i) {
44 ulong end; /* last address in current sect */
45
46 end = (i == s_end) ? b_end : info->start[i + 1] - 1;
47
48 /* Update protection if any part of the sector
49 * is in the specified range.
50 */
51 if (from <= end && to >= info->start[i]) {
52 if (flag & FLAG_PROTECT_CLEAR) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020053#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkc7de8292002-11-19 11:04:11 +000054 flash_real_protect(info, i, 0);
55#else
56 info->protect[i] = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020057#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenkc7de8292002-11-19 11:04:11 +000058 }
59 else if (flag & FLAG_PROTECT_SET) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020060#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkc7de8292002-11-19 11:04:11 +000061 flash_real_protect(info, i, 1);
62#else
63 info->protect[i] = 1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020064#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenkc7de8292002-11-19 11:04:11 +000065 }
66 }
67 }
68}
69
70/*-----------------------------------------------------------------------
71 */
72
73flash_info_t *
74addr2info (ulong addr)
75{
76#ifndef CONFIG_SPD823TS
77 flash_info_t *info;
78 int i;
79
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020080 for (i=0, info = &flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
wdenkc7de8292002-11-19 11:04:11 +000081 if (info->flash_id != FLASH_UNKNOWN &&
82 addr >= info->start[0] &&
83 /* WARNING - The '- 1' is needed if the flash
84 * is at the end of the address space, since
85 * info->start[0] + info->size wraps back to 0.
86 * Please don't change this unless you understand this.
87 */
88 addr <= info->start[0] + info->size - 1) {
89 return (info);
90 }
91 }
92#endif /* CONFIG_SPD823TS */
93
94 return (NULL);
95}
96
97/*-----------------------------------------------------------------------
98 * Copy memory to flash.
99 * Make sure all target addresses are within Flash bounds,
100 * and no protected sectors are hit.
101 * Returns:
102 * ERR_OK 0 - OK
103 * ERR_TIMOUT 1 - write timeout
104 * ERR_NOT_ERASED 2 - Flash not erased
105 * ERR_PROTECTED 4 - target range includes protected sectors
106 * ERR_INVAL 8 - target address not in Flash memory
107 * ERR_ALIGN 16 - target address not aligned on boundary
108 * (only some targets require alignment)
109 */
110int
Marian Balakowicz66080432006-09-01 19:46:22 +0200111flash_write (char *src, ulong addr, ulong cnt)
wdenkc7de8292002-11-19 11:04:11 +0000112{
113#ifdef CONFIG_SPD823TS
114 return (ERR_TIMOUT); /* any other error codes are possible as well */
115#else
116 int i;
117 ulong end = addr + cnt - 1;
118 flash_info_t *info_first = addr2info (addr);
119 flash_info_t *info_last = addr2info (end );
120 flash_info_t *info;
121 int j;
122
123 if (cnt == 0) {
124 return (ERR_OK);
125 }
126
127 if (!info_first || !info_last) {
128 return (ERR_INVAL);
129 }
130
131 for (info = info_first; info <= info_last; ++info) {
132 ulong b_end = info->start[0] + info->size; /* bank end addr */
133 short s_end = info->sector_count - 1;
134 for (i=0; i<info->sector_count; ++i) {
135 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
136
137 if ((end >= info->start[i]) && (addr < e_addr) &&
138 (info->protect[i] != 0) ) {
139 return (ERR_PROTECTED);
140 }
141 }
142 }
143
wdenk27b207f2003-07-24 23:38:38 +0000144 printf("\rWriting ");
145 for (j=0; j<20; j++) putc(177);
146 printf("\rWriting ");
wdenkc7de8292002-11-19 11:04:11 +0000147
148 /* finally write data to flash */
149 for (info = info_first; info <= info_last && cnt>0; ++info) {
150 ulong len;
151
152 len = info->start[0] + info->size - addr;
153 if (len > cnt)
154 len = cnt;
155
156 if ((i = write_buff(info, src, addr, len)) != 0) {
157 return (i);
158 }
159 cnt -= len;
160 addr += len;
161 src += len;
162 }
163 return (ERR_OK);
164#endif /* CONFIG_SPD823TS */
165}
166
167/*-----------------------------------------------------------------------
168 */