Trivial (cosmetic) cleanup:
[coreboot.git] / util / flashrom / 82802ab.c
1 /*
2  * 82802ab.c: driver for programming JEDEC standard flash parts
3  *
4  *
5  * Copyright 2000 Silicon Integrated System Corporation
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  *
22  * Reference: http://www.intel.com/design/chipsets/datashts/290658.htm
23  *
24  */
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/mman.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "flash.h"
34 #include "82802ab.h"
35 #include "debug.h"
36
37 // I need that Berkeley bit-map printer
38 void print_82802ab_status(uint8_t status)
39 {
40         printf("%s", status & 0x80 ? "Ready:" : "Busy:");
41         printf("%s", status & 0x40 ? "BE SUSPEND:" : "BE RUN/FINISH:");
42         printf("%s", status & 0x20 ? "BE ERROR:" : "BE OK:");
43         printf("%s", status & 0x10 ? "PROG ERR:" : "PROG OK:");
44         printf("%s", status & 0x8 ? "VP ERR:" : "VPP OK:");
45         printf("%s", status & 0x4 ? "PROG SUSPEND:" : "PROG RUN/FINISH:");
46         printf("%s", status & 0x2 ? "WP|TBL#|WP#,ABORT:" : "UNLOCK:");
47 }
48
49 int probe_82802ab(struct flashchip *flash)
50 {
51         volatile uint8_t *bios = flash->virt_addr;
52         uint8_t id1, id2;
53
54 #if 0
55         *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
56         *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
57         *(volatile uint8_t *) (bios + 0x5555) = 0x90;
58 #endif
59
60         *bios = 0xff;
61         myusec_delay(10);
62         *bios = 0x90;
63         myusec_delay(10);
64
65         id1 = *(volatile uint8_t *) bios;
66         id2 = *(volatile uint8_t *) (bios + 0x01);
67
68 #if 1
69         *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
70         *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
71         *(volatile uint8_t *) (bios + 0x5555) = 0xF0;
72
73 #endif
74         myusec_delay(10);
75
76         printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
77
78         if (id1 == flash->manufacture_id && id2 == flash->model_id) {
79                 size_t size = flash->total_size * 1024;
80                 // we need to mmap the write-protect space. 
81                 bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
82                             fd_mem, (off_t) (0 - 0x400000 - size));
83                 if (bios == MAP_FAILED) {
84                         // it's this part but we can't map it ...
85                         perror("Error MMAP memory using " MEM_DEV );
86                         exit(1);
87                 }
88
89                 flash->virt_addr_2 = bios;
90                 return 1;
91         }
92
93         return 0;
94 }
95
96 uint8_t wait_82802ab(volatile uint8_t *bios)
97 {
98
99         uint8_t status;
100         uint8_t id1, id2;
101
102         *bios = 0x70;
103         if ((*bios & 0x80) == 0) {      // it's busy
104                 while ((*bios & 0x80) == 0);
105         }
106
107         status = *bios;
108
109         // put another command to get out of status register mode
110
111         *bios = 0x90;
112         myusec_delay(10);
113
114         id1 = *(volatile uint8_t *) bios;
115         id2 = *(volatile uint8_t *) (bios + 0x01);
116
117         // this is needed to jam it out of "read id" mode
118         *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
119         *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
120         *(volatile uint8_t *) (bios + 0x5555) = 0xF0;
121         return status;
122
123 }
124 int erase_82802ab_block(struct flashchip *flash, int offset)
125 {
126         volatile uint8_t *bios = flash->virt_addr + offset;
127         volatile uint8_t *wrprotect =
128             flash->virt_addr_2 + offset + 2;
129         uint8_t status;
130
131         // clear status register
132         *bios = 0x50;
133         //printf("Erase at %p\n", bios);
134         // clear write protect
135         //printf("write protect is at %p\n", (wrprotect));
136         //printf("write protect is 0x%x\n", *(wrprotect));
137         *(wrprotect) = 0;
138         //printf("write protect is 0x%x\n", *(wrprotect));
139
140         // now start it
141         *(volatile uint8_t *) (bios) = 0x20;
142         *(volatile uint8_t *) (bios) = 0xd0;
143         myusec_delay(10);
144         // now let's see what the register is
145         status = wait_82802ab(flash->virt_addr);
146         //print_82802ab_status(status);
147         printf("DONE BLOCK 0x%x\n", offset);
148         return (0);
149 }
150 int erase_82802ab(struct flashchip *flash)
151 {
152         int i;
153         unsigned int total_size = flash->total_size * 1024;
154
155         printf("total_size is %d; flash->page_size is %d\n",
156                total_size, flash->page_size);
157         for (i = 0; i < total_size; i += flash->page_size)
158                 erase_82802ab_block(flash, i);
159         printf("DONE ERASE\n");
160         return (0);
161 }
162
163 void write_page_82802ab(volatile uint8_t *bios, uint8_t *src, volatile uint8_t *dst,
164                         int page_size)
165 {
166         int i;
167
168         for (i = 0; i < page_size; i++) {
169                 /* transfer data from source to destination */
170                 *dst = 0x40;
171                 *dst++ = *src++;
172                 wait_82802ab(bios);
173         }
174
175 }
176
177 int write_82802ab(struct flashchip *flash, uint8_t *buf)
178 {
179         int i;
180         int total_size = flash->total_size * 1024, page_size =
181             flash->page_size;
182         volatile uint8_t *bios = flash->virt_addr;
183
184         erase_82802ab(flash);
185         if (*bios != 0xff) {
186                 printf("ERASE FAILED\n");
187                 return -1;
188         }
189         printf("Programming Page: ");
190         for (i = 0; i < total_size / page_size; i++) {
191                 printf("%04d at address: 0x%08x", i, i * page_size);
192                 write_page_82802ab(bios, buf + i * page_size,
193                                    bios + i * page_size, page_size);
194                 printf
195                     ("\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");
196         }
197         printf("\n");
198         protect_82802ab(bios);
199         return (0);
200 }