summaryrefslogtreecommitdiff
path: root/src/mainboard/motorola/sandpoint/flash/amd800.c
blob: 4fc155b2fc25b5bcf800e5fba945a006756ac33d (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
/*
 * This file is part of the LinuxBIOS project.
 *
 * Copyright (C) 2000 AG Electronics Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <console/console.h>
#include <stdlib.h>
#include "../flash.h"

struct data_amd800
{
    unsigned base;
    unsigned spacing;
    unsigned cs;
    const char *tag;
};

static const char *identify_amd (struct flash_device *flash_device);
static int erase_flash_amd800 (void *data, unsigned offset, unsigned length);
static int program_flash_amd800 (void *data, unsigned offset, const void *source,
				 unsigned length);
static uint8_t read_byte_amd800(void *data, unsigned offset);

static flash_fn fn_amd800 = {
    identify_amd,
    0,
    0,
    erase_flash_amd800,
    program_flash_amd800,
    read_byte_amd800
};

const char *identify_amd (struct flash_device *flash_device)
{
    struct data_amd800 *d800 = flash_device->data;

    if (!d800->tag)
    {
	volatile unsigned char *flash =

	    (volatile unsigned char *) d800->base;
	unsigned char type,
	 id;

	*(flash + 0xaaa * d800->spacing) = 0xaa;
	*(flash + 0x555 * d800->spacing) = 0x55;
	*(flash + 0xaaa * d800->spacing) = 0x90;
	type = *(flash + 2 * d800->spacing);
	id = *flash;
	*flash = 0xf0;
	if ((id == 1 || id == 0x20) && type == 0x5b)
	{
	    d800->cs = 45;
	    d800->tag = "Am29LV800BB";
    	    flash_device->base = d800->base;
    	    flash_device->size = 1024*1024;
	    flash_device->erase_size = 64*1024;
	    flash_device->store_size = 1;
	}
	else
	{
	    printk_info("Unknown flash ID: 0x%02x 0x%02x\n", id, type);
	}
    }
    return d800->tag;
}

int erase_flash_amd800 (void *data, unsigned offset, unsigned length)
{
    struct data_amd800 *d800 = data;
    volatile unsigned char *flash = (volatile unsigned char *) d800->base;
    volatile unsigned char *flash_aaa = flash + 0xaaa * d800->spacing;
    volatile unsigned char *flash_555 = flash + 0x555 * d800->spacing;
    int id;
    int cs = 9999;

    printk_info("Erase from 0x%08x to 0x%08x\n", offset, offset + length);
    *flash_aaa = 0xAA;		// Chip Erase
    *flash_555 = 0x55;
    *flash_aaa = 0x80;
    *flash_aaa = 0xAA;
    *flash_555 = 0x55;
    *flash_aaa = 0x10;

    for (; cs > 0; cs--)
    {
	id = *(flash + 16);
	if (id & 0xA0)		// DQ7 or DQ5 set: done or error
	    break;
	printk_info("%4d\b\b\b\b", cs);
    }

    *flash_aaa = 0xF0;		// In case of error

    printk_info("\b\b\b\b    \b\b\b\b");
    if (cs == 0)
    {
	printk_info("Could not erase flash, timeout.\n");
	return -1;
    }
    else if ((id & 0x80) == 0)
    {
	printk_info("Could not erase flash, status=%02x.\n", id);
	return -1;
    }
    printk_info("Flash erased\n");
    return 0;
}

int init_flash_amd800 (char *tag, unsigned base, unsigned spacing)
{
    struct data_amd800 *data = malloc (sizeof (struct data_amd800));

    if (data)
    {
	data->base = base;
	data->spacing = spacing;
	data->tag = 0;
	if (register_flash_device (&fn_amd800, tag, data) < 0)
	{
	    free (data);
	    return -1;
	}
    }
    else
	return -1;
    return 0;
}

int program_flash_amd800 (void *data, unsigned offset, const void *source,
			  unsigned length)
{
    struct data_amd800 *d800 = data;
    volatile unsigned char *flash = (volatile unsigned char *) d800->base;
    volatile unsigned char *flash_aaa = flash + 0xaaa * d800->spacing;
    volatile unsigned char *flash_555 = flash + 0x555 * d800->spacing;
    int id = 0;
    int cs;
    int errs = 0;
    volatile char *s;
    volatile char *d;

    printk_info("Program from 0x%08x to 0x%08x\n", offset, offset + length);
    printk_info("Data at %p\n", source);

    *flash_aaa = 0xAA;		// Unlock Bypass
    *flash_555 = 0x55;
    *flash_aaa = 0x20;

    s = (unsigned char *) source;
    d = flash + offset * d800->spacing;
    cs = length;

    while (cs > 0 && !errs)
    {
	*flash = 0xA0;	// Unlock Bypass Program
	*d = *s;		// Program data

	while (1)
	{
	    id = *d;
	    if ((id & 0x80) == (*s & 0x80))	// DQ7 right? => program done
		break;
	    else if (id & 0x20)
	    {			// DQ5 set? => maybe errors
		id = *d;
		if ((id & 0x80) != (*s & 0x80))
		{
		    errs++;
		    break;
		}
	    }
	}

	// PRINT("Set %08lx = %02x\n", d, *d);

	s += 1;
	d += d800->spacing;
	cs--;
    }

    *flash = 0x90;		// Unlock Bypass Program Reset
    *flash = 0x00;
    *flash = 0xF0;

    if (errs != 0)
    {
	printk_info("FAIL: Status=%02x Address=%p.\n", id, d - d800->spacing);
	return -1;
    }
    printk_info("OK.\n");


    // Step 4: Verify the flash.

    printk_info("  Verifying flash : ...");
    errs = 0;
    s = (unsigned char *) source;
    d = flash + offset * d800->spacing;
    for (cs = 0; cs < length; cs++)
    {
	if (*s != *d)
	{
	    if (errs == 0)
		printk_info("ERROR: Addr: %08p, PCI: %02x Lcl: %02x.\n",
		       s, *s, *d);
	    errs++;
	}
	s += 1;
	d += d800->spacing;
    }

    if (errs == 0)
	printk_info("OK.\n");
    else
    {
	printk_info("  FAIL: %d errors.\n", errs);
	return -1;
    }

    return 0;
}

uint8_t read_byte_amd800 (void *data, unsigned offset)
{
    struct data_amd800 *d800 = data;
    volatile unsigned char *flash = (volatile unsigned char *) d800->base;
    return *(flash + offset * d800->spacing);
}