xref: /aosp_15_r20/external/flashrom/sst28sf040.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2000 Silicon Integrated System Corporation
5  * Copyright (C) 2005 coresystems GmbH <[email protected]>
6  * Copyright (C) 2009 Sean Nelson <[email protected]>
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 
19 #include "flash.h"
20 #include "chipdrivers.h"
21 
22 #define AUTO_PG_ERASE1		0x20
23 #define AUTO_PG_ERASE2		0xD0
24 #define AUTO_PGRM		0x10
25 #define CHIP_ERASE		0x30
26 #define RESET			0xFF
27 #define READ_ID			0x90
28 
protect_28sf040(struct flashctx * flash)29 int protect_28sf040(struct flashctx *flash)
30 {
31 	chipaddr bios = flash->virtual_memory;
32 
33 	chip_readb(flash, bios + 0x1823);
34 	chip_readb(flash, bios + 0x1820);
35 	chip_readb(flash, bios + 0x1822);
36 	chip_readb(flash, bios + 0x0418);
37 	chip_readb(flash, bios + 0x041B);
38 	chip_readb(flash, bios + 0x0419);
39 	chip_readb(flash, bios + 0x040A);
40 
41 	return 0;
42 }
43 
unprotect_28sf040(struct flashctx * flash)44 int unprotect_28sf040(struct flashctx *flash)
45 {
46 	chipaddr bios = flash->virtual_memory;
47 
48 	chip_readb(flash, bios + 0x1823);
49 	chip_readb(flash, bios + 0x1820);
50 	chip_readb(flash, bios + 0x1822);
51 	chip_readb(flash, bios + 0x0418);
52 	chip_readb(flash, bios + 0x041B);
53 	chip_readb(flash, bios + 0x0419);
54 	chip_readb(flash, bios + 0x041A);
55 
56 	return 0;
57 }
58 
erase_sector_28sf040(struct flashctx * flash,unsigned int address,unsigned int sector_size)59 int erase_sector_28sf040(struct flashctx *flash, unsigned int address,
60 			 unsigned int sector_size)
61 {
62 	chipaddr bios = flash->virtual_memory;
63 
64 	/* This command sequence is very similar to erase_block_82802ab. */
65 	chip_writeb(flash, AUTO_PG_ERASE1, bios);
66 	chip_writeb(flash, AUTO_PG_ERASE2, bios + address);
67 
68 	/* wait for Toggle bit ready */
69 	toggle_ready_jedec(flash, bios);
70 
71 	/* FIXME: Check the status register for errors. */
72 	return 0;
73 }
74 
75 /* chunksize is 1 */
write_28sf040(struct flashctx * flash,const uint8_t * src,unsigned int start,unsigned int len)76 int write_28sf040(struct flashctx *flash, const uint8_t *src, unsigned int start, unsigned int len)
77 {
78 	unsigned int i;
79 	chipaddr bios = flash->virtual_memory;
80 	chipaddr dst = flash->virtual_memory + start;
81 
82 	for (i = 0; i < len; i++) {
83 		/* transfer data from source to destination */
84 		if (*src == 0xFF) {
85 			dst++, src++;
86 			/* If the data is 0xFF, don't program it */
87 			continue;
88 		}
89 		/*issue AUTO PROGRAM command */
90 		chip_writeb(flash, AUTO_PGRM, dst);
91 		chip_writeb(flash, *src++, dst++);
92 
93 		/* wait for Toggle bit ready */
94 		toggle_ready_jedec(flash, bios);
95 		update_progress(flash, FLASHROM_PROGRESS_WRITE, i + 1, len);
96 	}
97 
98 	return 0;
99 }
100 
erase_28sf040(struct flashctx * flash)101 static int erase_28sf040(struct flashctx *flash)
102 {
103 	chipaddr bios = flash->virtual_memory;
104 
105 	chip_writeb(flash, CHIP_ERASE, bios);
106 	chip_writeb(flash, CHIP_ERASE, bios);
107 
108 	programmer_delay(flash, 10);
109 	toggle_ready_jedec(flash, bios);
110 
111 	/* FIXME: Check the status register for errors. */
112 	return 0;
113 }
114 
erase_chip_28sf040(struct flashctx * flash,unsigned int addr,unsigned int blocklen)115 int erase_chip_28sf040(struct flashctx *flash, unsigned int addr,
116 		       unsigned int blocklen)
117 {
118 	if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
119 		msg_cerr("%s called with incorrect arguments\n",
120 			__func__);
121 		return -1;
122 	}
123 	return erase_28sf040(flash);
124 }
125