There was a programming error which made most USB port4 setup wrong. This patch uses...
[coreboot.git] / util / flashrom / pm49fl00x.c
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2004 Tyan Corporation
5  * Copyright (C) 2007 Nikolay Petukhov <nikolay.petukhov@gmail.com>
6  * Copyright (C) 2007 Reinder E.N. de Haan <lb_reha@mveas.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include <stdio.h>
24 #include "flash.h"
25
26 extern int exclude_start_page, exclude_end_page;
27
28 void write_lockbits_49fl00x(volatile uint8_t *bios, int size,
29                                               unsigned char bits, int block_size)
30 {
31         int i, left = size;
32
33         for (i = 0; left >= block_size; i++, left -= block_size) {
34
35                 /* pm49fl002 */
36                 if (block_size == 16384 && i%2)
37                         continue;
38
39                 *(bios + (i * block_size) + 2) = bits;
40         }
41 }
42
43 int probe_49fl00x(struct flashchip *flash)
44 {
45         int ret = probe_jedec(flash);
46         
47         if (ret == 1)
48                 map_flash_registers(flash);
49
50         return ret;
51 }
52
53 int erase_49fl00x(struct flashchip *flash)
54 {
55         int i;
56         int total_size = flash->total_size * 1024;
57         int page_size = flash->page_size;
58         volatile uint8_t *bios = flash->virtual_memory;
59
60         /* unprotected */
61         write_lockbits_49fl00x(flash->virtual_registers, total_size, 0, page_size);
62
63         //erase_chip_jedec will not work ... datasheet says "Chip erase is available in A/A Mux Mode only"
64         printf("Erasing page: ");
65         for (i = 0; i < total_size / page_size; i++) {
66                 if ((i >= exclude_start_page) && (i < exclude_end_page))
67                         continue;
68
69                 /* erase the page */
70                 erase_block_jedec(bios, i * page_size);
71                 printf("%04d at address: 0x%08x", i, i * page_size);
72                 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");
73                 fflush(stdout);
74         }
75         printf("\n");
76
77         /* protected */
78         write_lockbits_49fl00x(flash->virtual_registers, total_size, 1, page_size);
79
80         return 0;
81 }
82
83 int write_49fl00x(struct flashchip *flash, uint8_t *buf)
84 {
85         int i;
86         int total_size = flash->total_size * 1024;
87         int page_size = flash->page_size;
88         volatile uint8_t *bios = flash->virtual_memory;
89
90         /* unprotected */
91         write_lockbits_49fl00x(flash->virtual_registers, total_size, 0, page_size);
92
93         printf("Programming page: ");
94         for (i = 0; i < total_size / page_size; i++) {
95                 if ((i >= exclude_start_page) && (i < exclude_end_page))
96                         continue;
97
98                 /* erase the page before programming */
99                 erase_block_jedec(bios, i * page_size);
100
101                 /* write to the sector */
102                 printf("%04d at address: 0x%08x", i, i * page_size);
103                 write_sector_jedec(bios, buf + i * page_size,
104                                    bios + i * page_size, page_size);
105                 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");
106                 fflush(stdout);
107         }
108         printf("\n");
109         
110         /* protected */
111         write_lockbits_49fl00x(flash->virtual_registers, total_size, 1, page_size);
112
113         return 0;
114 }