rmove unused #define and function declaretion
[coreboot.git] / util / flash_and_burn / pm49fl004.c
1 /*
2  * pm49fl004.c: driver for Pm49FL004 flash models.
3  *
4  *
5  * Copyright 2004 Tyan 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  */
23
24 #include <stdio.h>
25 #include "flash.h"
26 #include "jedec.h"
27 #include "pm49fl004.h"
28
29 static __inline__ int erase_block_49fl004(volatile unsigned char *bios,
30                                           unsigned long address)
31 {
32         volatile unsigned char *Temp;
33
34         Temp = bios + 0x5555;   /* set up address to be C000:5555h      */
35         *Temp = 0xAA;           /* write data 0xAA to the address       */
36         myusec_delay(10);
37         Temp = bios + 0x2AAA;   /* set up address to be C000:2AAAh      */
38         *Temp = 0x55;           /* write data 0x55 to the address       */
39         myusec_delay(10);
40         Temp = bios + 0x5555;   /* set up address to be C000:5555h      */
41         *Temp = 0x80;           /* write data 0x80 to the address       */
42         myusec_delay(10);
43         Temp = bios + 0x5555;   /* set up address to be C000:5555h      */
44         *Temp = 0xAA;           /* write data 0xAA to the address       */
45         myusec_delay(10);
46         Temp = bios + 0x2AAA;   /* set up address to be C000:2AAAh      */
47         *Temp = 0x55;           /* write data 0x55 to the address       */
48         myusec_delay(10);
49         Temp = bios + address;  /* set up address to be C000:5555h      */
50         *Temp = 0x50;           /* write data 0x50 to the address       */
51
52         /* wait for Toggle bit ready         */
53         toggle_ready_jedec(bios);
54
55         return (0);
56 }
57
58 static __inline__ int write_block_49fl004(volatile char *bios,
59                                           unsigned char *src,
60                                           volatile unsigned char *dst,
61                                           unsigned int page_size)
62 {
63         int i;
64         volatile char *Temp;
65
66         for (i = 0; i < page_size; i++) {
67                 if (*dst != 0xff) {
68                         printf("FATAL: dst %p not erased (val 0x%x\n", dst,
69                                *dst);
70                         return (-1);
71                 }
72                 /* transfer data from source to destination */
73                 if (*src == 0xFF) {
74                         dst++, src++;
75                         /* If the data is 0xFF, don't program it */
76                         continue;
77                 }
78                 Temp = (bios + 0x5555);
79                 *Temp = 0xAA;
80                 Temp = bios + 0x2AAA;
81                 *Temp = 0x55;
82                 Temp = bios + 0x5555;
83                 *Temp = 0xA0;
84                 *dst = *src;
85                 toggle_ready_jedec(bios);
86                 if (*dst != *src)
87                         printf("BAD! dst 0x%lx val 0x%x src 0x%x\n",
88                                (unsigned long) dst, *dst, *src);
89                 dst++, src++;
90         }
91
92         return (0);
93 }
94
95 int write_49fl004(struct flashchip *flash, unsigned char *buf)
96 {
97         int i;
98         int total_size = flash->total_size * 1024, page_size =
99             flash->page_size;
100         volatile char *bios = flash->virt_addr;
101
102         printf("Programming Page: ");
103         for (i = 0; i < total_size / page_size; i++) {
104                 /* erase the page before programming */
105                 erase_block_49fl004(bios, i * page_size);
106
107                 /* write to the sector */
108                 printf("%04d at address: 0x%08x", i, i * page_size);
109                 write_block_49fl004(bios, buf + i * page_size,
110                                     bios + i * page_size, page_size);
111                 printf
112                     ("\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");
113                 fflush(stdout);
114         }
115         printf("\n");
116
117         return (0);
118 }