blob: 0c905260d69210bd79d4e0d8ee273420b8543630 [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2005 coresystems GmbH <stepan@openbios.org>
Sean Nelson51c83fb2010-01-20 20:55:53 +00006 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00007 *
Uwe Hermannd1107642007-08-29 17:52:32 +00008 * 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.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000012 *
Uwe Hermannd1107642007-08-29 17:52:32 +000013 * 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.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000017 *
Uwe Hermannd1107642007-08-29 17:52:32 +000018 * 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
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000021 */
22
23#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000024#include "chipdrivers.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000025
26#define AUTO_PG_ERASE1 0x20
27#define AUTO_PG_ERASE2 0xD0
Ollie Lhocf29de82004-03-18 19:40:07 +000028#define AUTO_PGRM 0x10
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000029#define CHIP_ERASE 0x30
30#define RESET 0xFF
31#define READ_ID 0x90
32
Uwe Hermann09e04f72009-05-16 22:36:00 +000033static void protect_28sf040(chipaddr bios)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000034{
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000035 chip_readb(bios + 0x1823);
36 chip_readb(bios + 0x1820);
37 chip_readb(bios + 0x1822);
38 chip_readb(bios + 0x0418);
39 chip_readb(bios + 0x041B);
40 chip_readb(bios + 0x0419);
41 chip_readb(bios + 0x040A);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000042}
43
Uwe Hermann09e04f72009-05-16 22:36:00 +000044static void unprotect_28sf040(chipaddr bios)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000045{
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000046 chip_readb(bios + 0x1823);
47 chip_readb(bios + 0x1820);
48 chip_readb(bios + 0x1822);
49 chip_readb(bios + 0x0418);
50 chip_readb(bios + 0x041B);
51 chip_readb(bios + 0x0419);
52 chip_readb(bios + 0x041A);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000053}
54
Sean Nelson51c83fb2010-01-20 20:55:53 +000055int erase_sector_28sf040(struct flashchip *flash, unsigned int address, unsigned int sector_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000056{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000057 chipaddr bios = flash->virtual_memory;
58
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000059 chip_writeb(AUTO_PG_ERASE1, bios);
60 chip_writeb(AUTO_PG_ERASE2, bios + address);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000061
62 /* wait for Toggle bit ready */
63 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000064
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000065 if (check_erased_range(flash, address, sector_size)) {
66 fprintf(stderr, "ERASE FAILED!\n");
67 return -1;
68 }
Uwe Hermannffec5f32007-08-23 16:08:21 +000069 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000070}
71
Sean Nelson51c83fb2010-01-20 20:55:53 +000072int write_sector_28sf040(chipaddr bios, uint8_t *src, chipaddr dst,
Uwe Hermann09e04f72009-05-16 22:36:00 +000073 unsigned int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000074{
75 int i;
76
77 for (i = 0; i < page_size; i++) {
78 /* transfer data from source to destination */
79 if (*src == 0xFF) {
80 dst++, src++;
81 /* If the data is 0xFF, don't program it */
82 continue;
83 }
84 /*issue AUTO PROGRAM command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000085 chip_writeb(AUTO_PGRM, dst);
86 chip_writeb(*src++, dst++);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000087
88 /* wait for Toggle bit ready */
89 toggle_ready_jedec(bios);
90 }
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000091
Uwe Hermannffec5f32007-08-23 16:08:21 +000092 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000093}
94
Ollie Lho761bf1b2004-03-20 16:46:10 +000095int erase_28sf040(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000096{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000097 chipaddr bios = flash->virtual_memory;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000098
Ollie Lho761bf1b2004-03-20 16:46:10 +000099 unprotect_28sf040(bios);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000100 chip_writeb(CHIP_ERASE, bios);
101 chip_writeb(CHIP_ERASE, bios);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000102 protect_28sf040(bios);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000103
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000104 programmer_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000105 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000106
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000107 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
108 fprintf(stderr, "ERASE FAILED!\n");
109 return -1;
110 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000111 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000112}
113
Ollie Lho184a4042005-11-26 21:55:36 +0000114int write_28sf040(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000115{
116 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000117 int total_size = flash->total_size * 1024;
118 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000119 chipaddr bios = flash->virtual_memory;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000120
Ollie Lho761bf1b2004-03-20 16:46:10 +0000121 unprotect_28sf040(bios);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000122
Uwe Hermanna502dce2007-10-17 23:55:15 +0000123 printf("Programming page: ");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000124 for (i = 0; i < total_size / page_size; i++) {
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000125 /* erase the page before programming */
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000126 if (erase_sector_28sf040(flash, i * page_size, page_size)) {
127 fprintf(stderr, "ERASE FAILED!\n");
128 return -1;
129 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000130
131 /* write to the sector */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000132 printf("%04d at address: 0x%08x", i, i * page_size);
133 write_sector_28sf040(bios, buf + i * page_size,
134 bios + i * page_size, page_size);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000135 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");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000136 }
137 printf("\n");
138
Ollie Lho761bf1b2004-03-20 16:46:10 +0000139 protect_28sf040(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000140
Uwe Hermannffec5f32007-08-23 16:08:21 +0000141 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000142}
Sean Nelson51c83fb2010-01-20 20:55:53 +0000143
144int erase_chip_28sf040(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
145{
146 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
147 fprintf(stderr, "%s called with incorrect arguments\n",
148 __func__);
149 return -1;
150 }
151 return erase_28sf040(flash);
152}