85fb9d096672aaeba56b3f9558d508c641c2e2dd
[coreboot.git] / util / flashrom / sst28sf040.c
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2000 Silicon Integrated System Corporation
5  * Copyright (C) 2005 coresystems GmbH <stepan@openbios.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <stdio.h>
23 #include <stdint.h>
24 #include "flash.h"
25
26 #define AUTO_PG_ERASE1          0x20
27 #define AUTO_PG_ERASE2          0xD0
28 #define AUTO_PGRM               0x10
29 #define CHIP_ERASE              0x30
30 #define RESET                   0xFF
31 #define READ_ID                 0x90
32
33 static __inline__ void protect_28sf040(volatile uint8_t *bios)
34 {
35         /* ask compiler not to optimize this */
36         volatile uint8_t tmp;
37
38         tmp = *(volatile uint8_t *)(bios + 0x1823);
39         tmp = *(volatile uint8_t *)(bios + 0x1820);
40         tmp = *(volatile uint8_t *)(bios + 0x1822);
41         tmp = *(volatile uint8_t *)(bios + 0x0418);
42         tmp = *(volatile uint8_t *)(bios + 0x041B);
43         tmp = *(volatile uint8_t *)(bios + 0x0419);
44         tmp = *(volatile uint8_t *)(bios + 0x040A);
45 }
46
47 static __inline__ void unprotect_28sf040(volatile uint8_t *bios)
48 {
49         /* ask compiler not to optimize this */
50         volatile uint8_t tmp;
51
52         tmp = *(volatile uint8_t *)(bios + 0x1823);
53         tmp = *(volatile uint8_t *)(bios + 0x1820);
54         tmp = *(volatile uint8_t *)(bios + 0x1822);
55         tmp = *(volatile uint8_t *)(bios + 0x0418);
56         tmp = *(volatile uint8_t *)(bios + 0x041B);
57         tmp = *(volatile uint8_t *)(bios + 0x0419);
58         tmp = *(volatile uint8_t *)(bios + 0x041A);
59 }
60
61 static __inline__ int erase_sector_28sf040(volatile uint8_t *bios,
62                                            unsigned long address)
63 {
64         *bios = AUTO_PG_ERASE1;
65         *(bios + address) = AUTO_PG_ERASE2;
66
67         /* wait for Toggle bit ready         */
68         toggle_ready_jedec(bios);
69
70         return 0;
71 }
72
73 static __inline__ int write_sector_28sf040(volatile uint8_t *bios,
74                                            uint8_t *src,
75                                            volatile uint8_t *dst,
76                                            unsigned int page_size)
77 {
78         int i;
79
80         for (i = 0; i < page_size; i++) {
81                 /* transfer data from source to destination */
82                 if (*src == 0xFF) {
83                         dst++, src++;
84                         /* If the data is 0xFF, don't program it */
85                         continue;
86                 }
87                 /*issue AUTO PROGRAM command */
88                 *dst = AUTO_PGRM;
89                 *dst++ = *src++;
90
91                 /* wait for Toggle bit ready */
92                 toggle_ready_jedec(bios);
93         }
94
95         return 0;
96 }
97
98 int probe_28sf040(struct flashchip *flash)
99 {
100         volatile uint8_t *bios = flash->virtual_memory;
101         uint8_t id1, id2;
102
103         *bios = RESET;
104         myusec_delay(10);
105
106         *bios = READ_ID;
107         myusec_delay(10);
108         id1 = *(volatile uint8_t *)bios;
109         myusec_delay(10);
110         id2 = *(volatile uint8_t *)(bios + 0x01);
111
112         *bios = RESET;
113         myusec_delay(10);
114
115         printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
116         if (id1 == flash->manufacture_id && id2 == flash->model_id)
117                 return 1;
118
119         return 0;
120 }
121
122 int erase_28sf040(struct flashchip *flash)
123 {
124         volatile uint8_t *bios = flash->virtual_memory;
125
126         unprotect_28sf040(bios);
127         *bios = CHIP_ERASE;
128         *bios = CHIP_ERASE;
129         protect_28sf040(bios);
130
131         myusec_delay(10);
132         toggle_ready_jedec(bios);
133
134         return 0;
135 }
136
137 int write_28sf040(struct flashchip *flash, uint8_t *buf)
138 {
139         int i;
140         int total_size = flash->total_size * 1024;
141         int page_size = flash->page_size;
142         volatile uint8_t *bios = flash->virtual_memory;
143
144         unprotect_28sf040(bios);
145
146         printf("Programming page: ");
147         for (i = 0; i < total_size / page_size; i++) {
148                 /* erase the page before programming */
149                 erase_sector_28sf040(bios, i * page_size);
150
151                 /* write to the sector */
152                 printf("%04d at address: 0x%08x", i, i * page_size);
153                 write_sector_28sf040(bios, buf + i * page_size,
154                                      bios + i * page_size, page_size);
155                 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
156         }
157         printf("\n");
158
159         protect_28sf040(bios);
160
161         return 0;
162 }