blob: 5e5eec8358a145f45031c6f33d84e6f35a346e51 [file] [log] [blame]
Nikolay Petukhov4784c472008-05-17 01:08:58 +00001/*
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
Nikolay Petukhov4784c472008-05-17 01:08:58 +000023#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000024#include "chipdrivers.h"
Nikolay Petukhov4784c472008-05-17 01:08:58 +000025
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000026void write_lockbits_49fl00x(chipaddr bios, int size,
Uwe Hermann394131e2008-10-18 21:14:13 +000027 unsigned char bits, int block_size)
Nikolay Petukhov4784c472008-05-17 01:08:58 +000028{
29 int i, left = size;
30
31 for (i = 0; left >= block_size; i++, left -= block_size) {
Nikolay Petukhov4784c472008-05-17 01:08:58 +000032 /* pm49fl002 */
Uwe Hermann394131e2008-10-18 21:14:13 +000033 if (block_size == 16384 && i % 2)
Nikolay Petukhov4784c472008-05-17 01:08:58 +000034 continue;
35
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000036 chip_writeb(bits, bios + (i * block_size) + 2);
Nikolay Petukhov4784c472008-05-17 01:08:58 +000037 }
38}
39
Sean Nelson6e0b9122010-02-19 00:52:10 +000040int unlock_49fl00x(struct flashchip *flash)
41{
42 write_lockbits_49fl00x(flash->virtual_registers, flash->total_size * 1024, 0, flash->page_size);
43 return 0;
44}
45
Sean Nelson36172342010-02-27 18:01:15 +000046int lock_49fl00x(struct flashchip *flash)
47{
48 write_lockbits_49fl00x(flash->virtual_registers, flash->total_size * 1024, 1, flash->page_size);
49 return 0;
50}
51
Nikolay Petukhov4784c472008-05-17 01:08:58 +000052int erase_49fl00x(struct flashchip *flash)
53{
54 int i;
55 int total_size = flash->total_size * 1024;
56 int page_size = flash->page_size;
Nikolay Petukhov4784c472008-05-17 01:08:58 +000057
58 /* unprotected */
Uwe Hermann394131e2008-10-18 21:14:13 +000059 write_lockbits_49fl00x(flash->virtual_registers,
60 total_size, 0, page_size);
Nikolay Petukhov4784c472008-05-17 01:08:58 +000061
Uwe Hermann394131e2008-10-18 21:14:13 +000062 /*
63 * erase_chip_jedec() will not work... Datasheet says
64 * "Chip erase is available in A/A Mux Mode only".
65 */
Nikolay Petukhov4784c472008-05-17 01:08:58 +000066 printf("Erasing page: ");
67 for (i = 0; i < total_size / page_size; i++) {
Nikolay Petukhov4784c472008-05-17 01:08:58 +000068 /* erase the page */
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000069 if (erase_block_jedec(flash, i * page_size, page_size)) {
70 fprintf(stderr, "ERASE FAILED!\n");
71 return -1;
72 }
Nikolay Petukhov4784c472008-05-17 01:08:58 +000073 printf("%04d at address: 0x%08x", i, i * page_size);
74 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");
75 fflush(stdout);
76 }
77 printf("\n");
78
79 /* protected */
Uwe Hermann394131e2008-10-18 21:14:13 +000080 write_lockbits_49fl00x(flash->virtual_registers,
81 total_size, 1, page_size);
Nikolay Petukhov4784c472008-05-17 01:08:58 +000082
83 return 0;
84}
85
86int write_49fl00x(struct flashchip *flash, uint8_t *buf)
87{
88 int i;
89 int total_size = flash->total_size * 1024;
90 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000091 chipaddr bios = flash->virtual_memory;
Nikolay Petukhov4784c472008-05-17 01:08:58 +000092
93 /* unprotected */
Uwe Hermann394131e2008-10-18 21:14:13 +000094 write_lockbits_49fl00x(flash->virtual_registers, total_size, 0,
95 page_size);
Nikolay Petukhov4784c472008-05-17 01:08:58 +000096
97 printf("Programming page: ");
98 for (i = 0; i < total_size / page_size; i++) {
Nikolay Petukhov4784c472008-05-17 01:08:58 +000099 /* erase the page before programming */
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000100 if (erase_block_jedec(flash, i * page_size, page_size)) {
101 fprintf(stderr, "ERASE FAILED!\n");
102 return -1;
103 }
Nikolay Petukhov4784c472008-05-17 01:08:58 +0000104
105 /* write to the sector */
106 printf("%04d at address: 0x%08x", i, i * page_size);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000107 write_sector_jedec_common(flash, buf + i * page_size,
108 bios + i * page_size, page_size, 0xffff);
Nikolay Petukhov4784c472008-05-17 01:08:58 +0000109 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");
110 fflush(stdout);
111 }
112 printf("\n");
Uwe Hermann394131e2008-10-18 21:14:13 +0000113
Nikolay Petukhov4784c472008-05-17 01:08:58 +0000114 /* protected */
Uwe Hermann394131e2008-10-18 21:14:13 +0000115 write_lockbits_49fl00x(flash->virtual_registers, total_size, 1,
116 page_size);
Nikolay Petukhov4784c472008-05-17 01:08:58 +0000117
118 return 0;
119}