flashrom: Use helper functions to access flash chips.
[coreboot.git] / util / flashrom / en29f002a.c
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2007 Carl-Daniel Hailfinger
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 /*
22  * EN29F512 has 1C,21
23  * EN29F010 has 1C,20
24  * EN29F040A has 1C,04
25  * EN29LV010 has 1C,6E and uses short F0 reset sequence
26  * EN29LV040(A) has 1C,4F and uses short F0 reset sequence
27  */
28
29 #include <stdio.h>
30 #include <stdint.h>
31 #include "flash.h"
32
33 int probe_en29f512(struct flashchip *flash)
34 {
35         volatile uint8_t *bios = flash->virtual_memory;
36         uint8_t id1, id2;
37
38         writeb(0xAA, bios + 0x555);
39         writeb(0x55, bios + 0x2AA);
40         writeb(0x90, bios + 0x555);
41
42         myusec_delay(10);
43
44         id1 = readb(bios + 0x100);
45         id2 = readb(bios + 0x101);
46
47         /* exit by writing F0 anywhere? or the code below */
48         writeb(0xAA, bios + 0x555);
49         writeb(0x55, bios + 0x2AA);
50         writeb(0xF0, bios + 0x555);
51
52         printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
53
54         if (id1 == flash->manufacture_id && id2 == flash->model_id)
55                 return 1;
56
57         return 0;
58 }
59
60 /*
61  * EN29F002AT has 1C,92
62  * EN29F002AB has 1C,97
63  */
64
65 /* This does not seem to function properly for EN29F002NT. */
66 int probe_en29f002a(struct flashchip *flash)
67 {
68         volatile uint8_t *bios = flash->virtual_memory;
69         uint8_t id1, id2;
70
71         writeb(0xAA, bios + 0x555);
72         writeb(0x55, bios + 0xAAA);
73         writeb(0x90, bios + 0x555);
74
75         myusec_delay(10);
76
77         id1 = readb(bios + 0x100);
78         id2 = readb(bios + 0x101);
79
80         /* exit by writing F0 anywhere? or the code below */
81         writeb(0xAA, bios + 0x555);
82         writeb(0x55, bios + 0xAAA);
83         writeb(0xF0, bios + 0x555);
84
85         printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
86
87         if (id1 == flash->manufacture_id && id2 == flash->model_id)
88                 return 1;
89
90         return 0;
91 }
92
93 /* The EN29F002 chip needs repeated single byte writing, no block writing. */
94 int write_en29f002a(struct flashchip *flash, uint8_t *buf)
95 {
96         int i;
97         int total_size = flash->total_size * 1024;
98         volatile uint8_t *bios = flash->virtual_memory;
99         volatile uint8_t *dst = bios;
100
101         // *bios = 0xF0;
102         myusec_delay(10);
103         erase_chip_jedec(flash);
104
105         printf("Programming page: ");
106         for (i = 0; i < total_size; i++) {
107                 /* write to the sector */
108                 if ((i & 0xfff) == 0)
109                         printf("address: 0x%08lx", (unsigned long)i);
110                 writeb(0xAA, bios + 0x5555);
111                 writeb(0x55, bios + 0x2AAA);
112                 writeb(0xA0, bios + 0x5555);
113                 writeb(*buf++, dst++);
114
115                 /* wait for Toggle bit ready */
116                 toggle_ready_jedec(dst);
117
118                 if ((i & 0xfff) == 0)
119                         printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
120         }
121
122         printf("\n");
123         return 0;
124 }