Drop a bunch of useless header files, merge them into flash.h.
[coreboot.git] / util / flashrom / am29f040b.c
1 /*
2  * am29f040.c: driver for programming AMD am29f040b models
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  * Reference:
22  *      AMD Am29F040B data sheet
23  */
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include "flash.h"
28
29 static __inline__ int erase_sector_29f040b(volatile uint8_t *bios,
30                                            unsigned long address)
31 {
32         *(bios + 0x555) = 0xAA;
33         *(bios + 0x2AA) = 0x55;
34         *(bios + 0x555) = 0x80;
35         *(bios + 0x555) = 0xAA;
36         *(bios + 0x2AA) = 0x55;
37         *(bios + address) = 0x30;
38
39         sleep(2);
40
41         /* wait for Toggle bit ready         */
42         toggle_ready_jedec(bios + address);
43
44         return (0);
45 }
46
47 static __inline__ int write_sector_29f040b(volatile uint8_t *bios,
48                                            uint8_t *src,
49                                            volatile uint8_t *dst,
50                                            unsigned int page_size)
51 {
52         int i;
53
54         for (i = 0; i < page_size; i++) {
55                 if ((i & 0xfff) == 0xfff)
56                         printf("0x%08lx", (unsigned long)dst -
57                                (unsigned long)bios);
58
59                 *(bios + 0x555) = 0xAA;
60                 *(bios + 0x2AA) = 0x55;
61                 *(bios + 0x555) = 0xA0;
62                 *dst++ = *src++;
63
64                 /* wait for Toggle bit ready */
65                 toggle_ready_jedec(bios);
66
67                 if ((i & 0xfff) == 0xfff)
68                         printf("\b\b\b\b\b\b\b\b\b\b");
69         }
70
71         return (0);
72 }
73
74 int probe_29f040b(struct flashchip *flash)
75 {
76         volatile uint8_t *bios = flash->virtual_memory;
77         uint8_t id1, id2;
78
79         *(bios + 0x555) = 0xAA;
80         *(bios + 0x2AA) = 0x55;
81         *(bios + 0x555) = 0x90;
82
83         id1 = *bios;
84         id2 = *(bios + 0x01);
85
86         *bios = 0xF0;
87
88         myusec_delay(10);
89
90         printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
91         if (id1 == flash->manufacture_id && id2 == flash->model_id)
92                 return 1;
93
94         return 0;
95 }
96
97 int erase_29f040b(struct flashchip *flash)
98 {
99         volatile uint8_t *bios = flash->virtual_memory;
100
101         *(bios + 0x555) = 0xAA;
102         *(bios + 0x2AA) = 0x55;
103         *(bios + 0x555) = 0x80;
104         *(bios + 0x555) = 0xAA;
105         *(bios + 0x2AA) = 0x55;
106         *(bios + 0x555) = 0x10;
107
108         myusec_delay(10);
109         toggle_ready_jedec(bios);
110
111         return (0);
112 }
113
114 int write_29f040b(struct flashchip *flash, uint8_t *buf)
115 {
116         int i;
117         int total_size = flash->total_size * 1024;
118         int page_size = flash->page_size;
119         volatile uint8_t *bios = flash->virtual_memory;
120
121         printf("Programming page ");
122         for (i = 0; i < total_size / page_size; i++) {
123                 /* erase the page before programming */
124                 erase_sector_29f040b(bios, i * page_size);
125
126                 /* write to the sector */
127                 printf("%04d at address: ", i);
128                 write_sector_29f040b(bios, buf + i * page_size,
129                                      bios + i * page_size, page_size);
130                 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
131         }
132         printf("\n");
133
134         return (0);
135 }