blob: b61f4331694749472c37f677a85a74cf1138ff6c [file] [log] [blame]
stepand4b13752007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfingerb8f7e882008-01-19 00:04:46 +00004 * Copyright (C) 2007, 2008 Carl-Daniel Hailfinger
stepandbd3af12008-06-27 16:28:34 +00005 * Copyright (C) 2008 coresystems GmbH
stepand4b13752007-10-15 21:45:29 +00006 *
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; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * Contains the generic SPI framework
23 */
24
25#include <stdio.h>
26#include <pci/pci.h>
27#include <stdint.h>
28#include <string.h>
29#include "flash.h"
hailfinger78031562008-05-13 14:58:23 +000030#include "spi.h"
stepand4b13752007-10-15 21:45:29 +000031
hailfingerb8f7e882008-01-19 00:04:46 +000032void spi_prettyprint_status_register(struct flashchip *flash);
stepand4b13752007-10-15 21:45:29 +000033
uwefa98ca12008-10-18 21:14:13 +000034int spi_command(unsigned int writecnt, unsigned int readcnt,
35 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +000036{
stepan3bdf6182008-06-30 23:45:22 +000037 switch (flashbus) {
38 case BUS_TYPE_IT87XX_SPI:
uwefa98ca12008-10-18 21:14:13 +000039 return it8716f_spi_command(writecnt, readcnt, writearr,
40 readarr);
stepan3bdf6182008-06-30 23:45:22 +000041 case BUS_TYPE_ICH7_SPI:
42 case BUS_TYPE_ICH9_SPI:
43 case BUS_TYPE_VIA_SPI:
uwefa98ca12008-10-18 21:14:13 +000044 return ich_spi_command(writecnt, readcnt, writearr, readarr);
uwe17efbed2008-11-28 21:36:51 +000045 case BUS_TYPE_SB600_SPI:
46 return sb600_spi_command(writecnt, readcnt, writearr, readarr);
stugea564bcf2009-01-26 03:08:45 +000047 case BUS_TYPE_WBSIO_SPI:
48 return wbsio_spi_command(writecnt, readcnt, writearr, readarr);
stepan3bdf6182008-06-30 23:45:22 +000049 default:
uwefa98ca12008-10-18 21:14:13 +000050 printf_debug
51 ("%s called, but no SPI chipset/strapping detected\n",
52 __FUNCTION__);
stepan3bdf6182008-06-30 23:45:22 +000053 }
hailfinger35cc8162007-10-16 21:09:06 +000054 return 1;
55}
56
ruikdbe18ee2008-06-30 21:45:17 +000057static int spi_rdid(unsigned char *readarr, int bytes)
stepand4b13752007-10-15 21:45:29 +000058{
uwefa98ca12008-10-18 21:14:13 +000059 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
hailfinger54c14662009-05-13 11:40:08 +000060 int ret;
stepand4b13752007-10-15 21:45:29 +000061
hailfinger54c14662009-05-13 11:40:08 +000062 ret = spi_command(sizeof(cmd), bytes, cmd, readarr);
63 if (ret)
64 return ret;
uwefa98ca12008-10-18 21:14:13 +000065 printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1],
66 readarr[2]);
stepand4b13752007-10-15 21:45:29 +000067 return 0;
68}
69
hailfinger3dd0c3e2008-11-28 01:25:00 +000070static int spi_rems(unsigned char *readarr)
71{
hailfinger54c14662009-05-13 11:40:08 +000072 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
73 uint32_t readaddr;
74 int ret;
hailfinger3dd0c3e2008-11-28 01:25:00 +000075
hailfinger54c14662009-05-13 11:40:08 +000076 ret = spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
77 if (ret == SPI_INVALID_ADDRESS) {
78 /* Find the lowest even address allowed for reads. */
79 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
80 cmd[1] = (readaddr >> 16) & 0xff,
81 cmd[2] = (readaddr >> 8) & 0xff,
82 cmd[3] = (readaddr >> 0) & 0xff,
83 ret = spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
84 }
85 if (ret)
86 return ret;
hailfinger3dd0c3e2008-11-28 01:25:00 +000087 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
88 return 0;
89}
90
hailfinger82893122008-05-15 03:19:49 +000091static int spi_res(unsigned char *readarr)
92{
hailfinger54c14662009-05-13 11:40:08 +000093 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
94 uint32_t readaddr;
95 int ret;
hailfinger82893122008-05-15 03:19:49 +000096
hailfinger54c14662009-05-13 11:40:08 +000097 ret = spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
98 if (ret == SPI_INVALID_ADDRESS) {
99 /* Find the lowest even address allowed for reads. */
100 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
101 cmd[1] = (readaddr >> 16) & 0xff,
102 cmd[2] = (readaddr >> 8) & 0xff,
103 cmd[3] = (readaddr >> 0) & 0xff,
104 ret = spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
105 }
106 if (ret)
107 return ret;
hailfinger82893122008-05-15 03:19:49 +0000108 printf_debug("RES returned %02x.\n", readarr[0]);
109 return 0;
110}
111
uwe5e931bc2009-04-15 10:52:49 +0000112int spi_write_enable(void)
hailfingerf71c0ac2007-10-18 00:24:07 +0000113{
uwefa98ca12008-10-18 21:14:13 +0000114 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
hailfinger61949942009-05-09 02:09:45 +0000115 int result;
hailfingerf71c0ac2007-10-18 00:24:07 +0000116
117 /* Send WREN (Write Enable) */
hailfinger61949942009-05-09 02:09:45 +0000118 result = spi_command(sizeof(cmd), 0, cmd, NULL);
119 if (result) {
120 printf_debug("spi_write_enable failed");
121 switch (flashbus) {
122 case BUS_TYPE_ICH7_SPI:
123 case BUS_TYPE_ICH9_SPI:
124 case BUS_TYPE_VIA_SPI:
125 printf_debug(" due to SPI master limitation, ignoring"
126 " and hoping it will be run as PREOP\n");
127 return 0;
128 default:
129 printf_debug("\n");
130 }
131 }
132 return result;
hailfingerf71c0ac2007-10-18 00:24:07 +0000133}
134
uwe5e931bc2009-04-15 10:52:49 +0000135int spi_write_disable(void)
hailfingerf71c0ac2007-10-18 00:24:07 +0000136{
uwefa98ca12008-10-18 21:14:13 +0000137 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
hailfingerf71c0ac2007-10-18 00:24:07 +0000138
139 /* Send WRDI (Write Disable) */
hailfingerc1b2e912008-11-18 00:41:02 +0000140 return spi_command(sizeof(cmd), 0, cmd, NULL);
hailfingerf71c0ac2007-10-18 00:24:07 +0000141}
142
ruikdbe18ee2008-06-30 21:45:17 +0000143static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
stepand4b13752007-10-15 21:45:29 +0000144{
ruikdbe18ee2008-06-30 21:45:17 +0000145 unsigned char readarr[4];
hailfinger492e3172008-02-06 22:07:58 +0000146 uint32_t manuf_id;
147 uint32_t model_id;
hailfingerf1961cb2007-12-29 10:15:58 +0000148
ruikdbe18ee2008-06-30 21:45:17 +0000149 if (spi_rdid(readarr, bytes))
stuge7be66832008-06-24 01:22:03 +0000150 return 0;
151
152 if (!oddparity(readarr[0]))
153 printf_debug("RDID byte 0 parity violation.\n");
154
155 /* Check if this is a continuation vendor ID */
156 if (readarr[0] == 0x7f) {
157 if (!oddparity(readarr[1]))
158 printf_debug("RDID byte 1 parity violation.\n");
159 manuf_id = (readarr[0] << 8) | readarr[1];
160 model_id = readarr[2];
ruikdbe18ee2008-06-30 21:45:17 +0000161 if (bytes > 3) {
162 model_id <<= 8;
163 model_id |= readarr[3];
164 }
stuge7be66832008-06-24 01:22:03 +0000165 } else {
166 manuf_id = readarr[0];
167 model_id = (readarr[1] << 8) | readarr[2];
stepand4b13752007-10-15 21:45:29 +0000168 }
169
stugef45dc842009-01-25 23:52:45 +0000170 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, manuf_id,
uwefa98ca12008-10-18 21:14:13 +0000171 model_id);
stuge7be66832008-06-24 01:22:03 +0000172
uwefa98ca12008-10-18 21:14:13 +0000173 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
stuge7be66832008-06-24 01:22:03 +0000174 /* Print the status register to tell the
175 * user about possible write protection.
176 */
177 spi_prettyprint_status_register(flash);
178
179 return 1;
180 }
181
182 /* Test if this is a pure vendor match. */
183 if (manuf_id == flash->manufacture_id &&
184 GENERIC_DEVICE_ID == flash->model_id)
185 return 1;
186
stepand4b13752007-10-15 21:45:29 +0000187 return 0;
188}
189
uwefa98ca12008-10-18 21:14:13 +0000190int probe_spi_rdid(struct flashchip *flash)
191{
ruikdbe18ee2008-06-30 21:45:17 +0000192 return probe_spi_rdid_generic(flash, 3);
193}
194
195/* support 4 bytes flash ID */
uwefa98ca12008-10-18 21:14:13 +0000196int probe_spi_rdid4(struct flashchip *flash)
197{
ruikdbe18ee2008-06-30 21:45:17 +0000198 /* only some SPI chipsets support 4 bytes commands */
stepan3bdf6182008-06-30 23:45:22 +0000199 switch (flashbus) {
200 case BUS_TYPE_ICH7_SPI:
201 case BUS_TYPE_ICH9_SPI:
202 case BUS_TYPE_VIA_SPI:
uwe17efbed2008-11-28 21:36:51 +0000203 case BUS_TYPE_SB600_SPI:
stugea564bcf2009-01-26 03:08:45 +0000204 case BUS_TYPE_WBSIO_SPI:
stepan3bdf6182008-06-30 23:45:22 +0000205 return probe_spi_rdid_generic(flash, 4);
206 default:
207 printf_debug("4b ID not supported on this SPI controller\n");
208 }
209
210 return 0;
ruikdbe18ee2008-06-30 21:45:17 +0000211}
212
hailfinger3dd0c3e2008-11-28 01:25:00 +0000213int probe_spi_rems(struct flashchip *flash)
214{
215 unsigned char readarr[JEDEC_REMS_INSIZE];
216 uint32_t manuf_id, model_id;
217
218 if (spi_rems(readarr))
219 return 0;
220
221 manuf_id = readarr[0];
222 model_id = readarr[1];
223
224 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id,
225 model_id);
226
227 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
228 /* Print the status register to tell the
229 * user about possible write protection.
230 */
231 spi_prettyprint_status_register(flash);
232
233 return 1;
234 }
235
236 /* Test if this is a pure vendor match. */
237 if (manuf_id == flash->manufacture_id &&
238 GENERIC_DEVICE_ID == flash->model_id)
239 return 1;
240
241 return 0;
242}
243
hailfinger82893122008-05-15 03:19:49 +0000244int probe_spi_res(struct flashchip *flash)
245{
246 unsigned char readarr[3];
247 uint32_t model_id;
stuge7be66832008-06-24 01:22:03 +0000248
hailfinger915cc852008-11-27 22:48:48 +0000249 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
250 * In that case, RES is pointless.
251 */
252 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
253 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
stuge7be66832008-06-24 01:22:03 +0000254 return 0;
hailfinger82893122008-05-15 03:19:49 +0000255
stuge7be66832008-06-24 01:22:03 +0000256 if (spi_res(readarr))
257 return 0;
258
259 model_id = readarr[0];
260 printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
261 if (model_id != flash->model_id)
262 return 0;
263
264 /* Print the status register to tell the
265 * user about possible write protection.
266 */
267 spi_prettyprint_status_register(flash);
268 return 1;
hailfinger82893122008-05-15 03:19:49 +0000269}
270
uwe5e931bc2009-04-15 10:52:49 +0000271uint8_t spi_read_status_register(void)
hailfingerf71c0ac2007-10-18 00:24:07 +0000272{
uwefa98ca12008-10-18 21:14:13 +0000273 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
stugea564bcf2009-01-26 03:08:45 +0000274 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
hailfinger54c14662009-05-13 11:40:08 +0000275 int ret;
hailfingerf71c0ac2007-10-18 00:24:07 +0000276
277 /* Read Status Register */
uwe17efbed2008-11-28 21:36:51 +0000278 if (flashbus == BUS_TYPE_SB600_SPI) {
279 /* SB600 uses a different way to read status register. */
280 return sb600_read_status_register();
281 } else {
hailfinger54c14662009-05-13 11:40:08 +0000282 ret = spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
283 if (ret)
284 printf_debug("RDSR failed!\n");
uwe17efbed2008-11-28 21:36:51 +0000285 }
286
hailfingerf71c0ac2007-10-18 00:24:07 +0000287 return readarr[0];
288}
289
uwe5e931bc2009-04-15 10:52:49 +0000290/* Prettyprint the status register. Common definitions. */
hailfingerb8f7e882008-01-19 00:04:46 +0000291void spi_prettyprint_status_register_common(uint8_t status)
292{
293 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
uwefa98ca12008-10-18 21:14:13 +0000294 "%sset\n", (status & (1 << 5)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000295 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
uwefa98ca12008-10-18 21:14:13 +0000296 "%sset\n", (status & (1 << 4)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000297 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
uwefa98ca12008-10-18 21:14:13 +0000298 "%sset\n", (status & (1 << 3)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000299 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
uwefa98ca12008-10-18 21:14:13 +0000300 "%sset\n", (status & (1 << 2)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000301 printf_debug("Chip status register: Write Enable Latch (WEL) is "
uwefa98ca12008-10-18 21:14:13 +0000302 "%sset\n", (status & (1 << 1)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000303 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
uwefa98ca12008-10-18 21:14:13 +0000304 "%sset\n", (status & (1 << 0)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000305}
306
hailfingerf1961cb2007-12-29 10:15:58 +0000307/* Prettyprint the status register. Works for
308 * ST M25P series
309 * MX MX25L series
310 */
hailfingerb8f7e882008-01-19 00:04:46 +0000311void spi_prettyprint_status_register_st_m25p(uint8_t status)
hailfingerf1961cb2007-12-29 10:15:58 +0000312{
313 printf_debug("Chip status register: Status Register Write Disable "
uwefa98ca12008-10-18 21:14:13 +0000314 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
hailfingerf1961cb2007-12-29 10:15:58 +0000315 printf_debug("Chip status register: Bit 6 is "
uwefa98ca12008-10-18 21:14:13 +0000316 "%sset\n", (status & (1 << 6)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000317 spi_prettyprint_status_register_common(status);
hailfingerf1961cb2007-12-29 10:15:58 +0000318}
319
hailfinger29c5caa2009-05-06 13:59:44 +0000320void spi_prettyprint_status_register_sst25(uint8_t status)
321{
322 printf_debug("Chip status register: Block Protect Write Disable "
323 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
324 printf_debug("Chip status register: Auto Address Increment Programming "
325 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
326 spi_prettyprint_status_register_common(status);
327}
328
hailfingerb8f7e882008-01-19 00:04:46 +0000329/* Prettyprint the status register. Works for
330 * SST 25VF016
331 */
332void spi_prettyprint_status_register_sst25vf016(uint8_t status)
333{
hailfinger9cd4cf12008-01-22 14:37:31 +0000334 const char *bpt[] = {
hailfingerb8f7e882008-01-19 00:04:46 +0000335 "none",
336 "1F0000H-1FFFFFH",
337 "1E0000H-1FFFFFH",
338 "1C0000H-1FFFFFH",
339 "180000H-1FFFFFH",
340 "100000H-1FFFFFH",
hailfinger9cd4cf12008-01-22 14:37:31 +0000341 "all", "all"
hailfingerb8f7e882008-01-19 00:04:46 +0000342 };
hailfinger29c5caa2009-05-06 13:59:44 +0000343 spi_prettyprint_status_register_sst25(status);
hailfingerb8f7e882008-01-19 00:04:46 +0000344 printf_debug("Resulting block protection : %s\n",
uwefa98ca12008-10-18 21:14:13 +0000345 bpt[(status & 0x1c) >> 2]);
hailfingerb8f7e882008-01-19 00:04:46 +0000346}
347
stuge36539392009-01-26 03:23:50 +0000348void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
349{
350 const char *bpt[] = {
351 "none",
352 "0x70000-0x7ffff",
353 "0x60000-0x7ffff",
354 "0x40000-0x7ffff",
355 "all blocks", "all blocks", "all blocks", "all blocks"
356 };
hailfinger29c5caa2009-05-06 13:59:44 +0000357 spi_prettyprint_status_register_sst25(status);
stuge36539392009-01-26 03:23:50 +0000358 printf_debug("Resulting block protection : %s\n",
hailfinger29c5caa2009-05-06 13:59:44 +0000359 bpt[(status & 0x1c) >> 2]);
stuge36539392009-01-26 03:23:50 +0000360}
361
hailfingerb8f7e882008-01-19 00:04:46 +0000362void spi_prettyprint_status_register(struct flashchip *flash)
hailfingerf1961cb2007-12-29 10:15:58 +0000363{
364 uint8_t status;
365
stuge2bb6ab32008-05-10 23:07:52 +0000366 status = spi_read_status_register();
hailfingerf1961cb2007-12-29 10:15:58 +0000367 printf_debug("Chip status register is %02x\n", status);
368 switch (flash->manufacture_id) {
369 case ST_ID:
hailfinger8b869132008-05-15 22:32:08 +0000370 if (((flash->model_id & 0xff00) == 0x2000) ||
371 ((flash->model_id & 0xff00) == 0x2500))
372 spi_prettyprint_status_register_st_m25p(status);
373 break;
hailfingerf1961cb2007-12-29 10:15:58 +0000374 case MX_ID:
375 if ((flash->model_id & 0xff00) == 0x2000)
hailfingerb8f7e882008-01-19 00:04:46 +0000376 spi_prettyprint_status_register_st_m25p(status);
377 break;
378 case SST_ID:
stuge36539392009-01-26 03:23:50 +0000379 switch (flash->model_id) {
380 case 0x2541:
hailfingerb8f7e882008-01-19 00:04:46 +0000381 spi_prettyprint_status_register_sst25vf016(status);
stuge36539392009-01-26 03:23:50 +0000382 break;
383 case 0x8d:
384 case 0x258d:
385 spi_prettyprint_status_register_sst25vf040b(status);
386 break;
hailfinger29c5caa2009-05-06 13:59:44 +0000387 case 0x258e:
388 spi_prettyprint_status_register_sst25(status);
389 break;
stuge36539392009-01-26 03:23:50 +0000390 }
hailfingerf1961cb2007-12-29 10:15:58 +0000391 break;
392 }
393}
uwefa98ca12008-10-18 21:14:13 +0000394
hailfingerffcf81a2008-11-03 00:02:11 +0000395int spi_chip_erase_60(struct flashchip *flash)
396{
397 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
hailfingerc1b2e912008-11-18 00:41:02 +0000398 int result;
hailfingerffcf81a2008-11-03 00:02:11 +0000399
hailfingerc1b2e912008-11-18 00:41:02 +0000400 result = spi_disable_blockprotect();
401 if (result) {
402 printf_debug("spi_disable_blockprotect failed\n");
403 return result;
404 }
405 result = spi_write_enable();
hailfinger61949942009-05-09 02:09:45 +0000406 if (result)
hailfingerc1b2e912008-11-18 00:41:02 +0000407 return result;
hailfingerffcf81a2008-11-03 00:02:11 +0000408 /* Send CE (Chip Erase) */
hailfingerc1b2e912008-11-18 00:41:02 +0000409 result = spi_command(sizeof(cmd), 0, cmd, NULL);
410 if (result) {
411 printf_debug("spi_chip_erase_60 failed sending erase\n");
412 return result;
413 }
hailfingerffcf81a2008-11-03 00:02:11 +0000414 /* Wait until the Write-In-Progress bit is cleared.
415 * This usually takes 1-85 s, so wait in 1 s steps.
416 */
hailfingerc1b2e912008-11-18 00:41:02 +0000417 /* FIXME: We assume spi_read_status_register will never fail. */
hailfingerffcf81a2008-11-03 00:02:11 +0000418 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
419 sleep(1);
420 return 0;
421}
422
stuge2bb6ab32008-05-10 23:07:52 +0000423int spi_chip_erase_c7(struct flashchip *flash)
hailfingerf71c0ac2007-10-18 00:24:07 +0000424{
uwefa98ca12008-10-18 21:14:13 +0000425 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
hailfingerc1b2e912008-11-18 00:41:02 +0000426 int result;
uwefa98ca12008-10-18 21:14:13 +0000427
hailfingerc1b2e912008-11-18 00:41:02 +0000428 result = spi_disable_blockprotect();
429 if (result) {
430 printf_debug("spi_disable_blockprotect failed\n");
431 return result;
432 }
433 result = spi_write_enable();
hailfinger61949942009-05-09 02:09:45 +0000434 if (result)
hailfingerc1b2e912008-11-18 00:41:02 +0000435 return result;
hailfingerf71c0ac2007-10-18 00:24:07 +0000436 /* Send CE (Chip Erase) */
hailfingerc1b2e912008-11-18 00:41:02 +0000437 result = spi_command(sizeof(cmd), 0, cmd, NULL);
438 if (result) {
439 printf_debug("spi_chip_erase_60 failed sending erase\n");
440 return result;
441 }
hailfinger1b24dbb2007-10-22 16:15:28 +0000442 /* Wait until the Write-In-Progress bit is cleared.
443 * This usually takes 1-85 s, so wait in 1 s steps.
444 */
hailfingerc1b2e912008-11-18 00:41:02 +0000445 /* FIXME: We assume spi_read_status_register will never fail. */
stuge2bb6ab32008-05-10 23:07:52 +0000446 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingerf71c0ac2007-10-18 00:24:07 +0000447 sleep(1);
hailfingerf71c0ac2007-10-18 00:24:07 +0000448 return 0;
449}
450
hailfingerc1b2e912008-11-18 00:41:02 +0000451int spi_chip_erase_60_c7(struct flashchip *flash)
452{
453 int result;
454 result = spi_chip_erase_60(flash);
455 if (result) {
456 printf_debug("spi_chip_erase_60 failed, trying c7\n");
457 result = spi_chip_erase_c7(flash);
458 }
459 return result;
460}
461
hailfingerffcf81a2008-11-03 00:02:11 +0000462int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
463{
464 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
hailfinger61949942009-05-09 02:09:45 +0000465 int result;
hailfingerffcf81a2008-11-03 00:02:11 +0000466
467 cmd[1] = (addr & 0x00ff0000) >> 16;
468 cmd[2] = (addr & 0x0000ff00) >> 8;
469 cmd[3] = (addr & 0x000000ff);
hailfinger61949942009-05-09 02:09:45 +0000470 result = spi_write_enable();
471 if (result)
472 return result;
hailfingerffcf81a2008-11-03 00:02:11 +0000473 /* Send BE (Block Erase) */
474 spi_command(sizeof(cmd), 0, cmd, NULL);
475 /* Wait until the Write-In-Progress bit is cleared.
476 * This usually takes 100-4000 ms, so wait in 100 ms steps.
477 */
478 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
479 usleep(100 * 1000);
480 return 0;
481}
482
hailfinger1b24dbb2007-10-22 16:15:28 +0000483/* Block size is usually
484 * 64k for Macronix
485 * 32k for SST
486 * 4-32k non-uniform for EON
487 */
stuge2bb6ab32008-05-10 23:07:52 +0000488int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
hailfinger1b24dbb2007-10-22 16:15:28 +0000489{
uwefa98ca12008-10-18 21:14:13 +0000490 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
hailfinger61949942009-05-09 02:09:45 +0000491 int result;
hailfinger1b24dbb2007-10-22 16:15:28 +0000492
493 cmd[1] = (addr & 0x00ff0000) >> 16;
494 cmd[2] = (addr & 0x0000ff00) >> 8;
495 cmd[3] = (addr & 0x000000ff);
hailfinger61949942009-05-09 02:09:45 +0000496 result = spi_write_enable();
497 if (result)
498 return result;
hailfinger1b24dbb2007-10-22 16:15:28 +0000499 /* Send BE (Block Erase) */
stuge494b4eb2008-07-07 06:38:51 +0000500 spi_command(sizeof(cmd), 0, cmd, NULL);
hailfinger1b24dbb2007-10-22 16:15:28 +0000501 /* Wait until the Write-In-Progress bit is cleared.
502 * This usually takes 100-4000 ms, so wait in 100 ms steps.
503 */
stuge2bb6ab32008-05-10 23:07:52 +0000504 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfinger1b24dbb2007-10-22 16:15:28 +0000505 usleep(100 * 1000);
506 return 0;
507}
508
stepan0f7bff02008-10-29 22:13:20 +0000509int spi_chip_erase_d8(struct flashchip *flash)
510{
511 int i, rc = 0;
512 int total_size = flash->total_size * 1024;
513 int erase_size = 64 * 1024;
514
515 spi_disable_blockprotect();
516
517 printf("Erasing chip: \n");
518
519 for (i = 0; i < total_size / erase_size; i++) {
520 rc = spi_block_erase_d8(flash, i * erase_size);
521 if (rc) {
522 printf("Error erasing block at 0x%x\n", i);
523 break;
524 }
525 }
526
527 printf("\n");
528
529 return rc;
530}
531
hailfinger1b24dbb2007-10-22 16:15:28 +0000532/* Sector size is usually 4k, though Macronix eliteflash has 64k */
stuge2bb6ab32008-05-10 23:07:52 +0000533int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
hailfinger1b24dbb2007-10-22 16:15:28 +0000534{
uwefa98ca12008-10-18 21:14:13 +0000535 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
hailfinger61949942009-05-09 02:09:45 +0000536 int result;
537
hailfinger1b24dbb2007-10-22 16:15:28 +0000538 cmd[1] = (addr & 0x00ff0000) >> 16;
539 cmd[2] = (addr & 0x0000ff00) >> 8;
540 cmd[3] = (addr & 0x000000ff);
541
hailfinger61949942009-05-09 02:09:45 +0000542 result = spi_write_enable();
543 if (result)
544 return result;
hailfinger1b24dbb2007-10-22 16:15:28 +0000545 /* Send SE (Sector Erase) */
stuge494b4eb2008-07-07 06:38:51 +0000546 spi_command(sizeof(cmd), 0, cmd, NULL);
hailfinger1b24dbb2007-10-22 16:15:28 +0000547 /* Wait until the Write-In-Progress bit is cleared.
548 * This usually takes 15-800 ms, so wait in 10 ms steps.
549 */
stuge2bb6ab32008-05-10 23:07:52 +0000550 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfinger1b24dbb2007-10-22 16:15:28 +0000551 usleep(10 * 1000);
552 return 0;
553}
554
uwe5e931bc2009-04-15 10:52:49 +0000555int spi_write_status_enable(void)
uwe17efbed2008-11-28 21:36:51 +0000556{
557 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
558
559 /* Send EWSR (Enable Write Status Register). */
560 return spi_command(JEDEC_EWSR_OUTSIZE, JEDEC_EWSR_INSIZE, cmd, NULL);
561}
562
hailfingerb8f7e882008-01-19 00:04:46 +0000563/*
564 * This is according the SST25VF016 datasheet, who knows it is more
565 * generic that this...
566 */
hailfingerc1b2e912008-11-18 00:41:02 +0000567int spi_write_status_register(int status)
hailfingerb8f7e882008-01-19 00:04:46 +0000568{
uwefa98ca12008-10-18 21:14:13 +0000569 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
570 { JEDEC_WRSR, (unsigned char)status };
hailfingerb8f7e882008-01-19 00:04:46 +0000571
572 /* Send WRSR (Write Status Register) */
hailfingerc1b2e912008-11-18 00:41:02 +0000573 return spi_command(sizeof(cmd), 0, cmd, NULL);
hailfingerb8f7e882008-01-19 00:04:46 +0000574}
575
576void spi_byte_program(int address, uint8_t byte)
577{
uwefa98ca12008-10-18 21:14:13 +0000578 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
579 JEDEC_BYTE_PROGRAM,
580 (address >> 16) & 0xff,
581 (address >> 8) & 0xff,
582 (address >> 0) & 0xff,
hailfingerb8f7e882008-01-19 00:04:46 +0000583 byte
584 };
585
586 /* Send Byte-Program */
stuge494b4eb2008-07-07 06:38:51 +0000587 spi_command(sizeof(cmd), 0, cmd, NULL);
hailfingerb8f7e882008-01-19 00:04:46 +0000588}
589
hailfingerc1b2e912008-11-18 00:41:02 +0000590int spi_disable_blockprotect(void)
hailfingerb8f7e882008-01-19 00:04:46 +0000591{
592 uint8_t status;
hailfingerc1b2e912008-11-18 00:41:02 +0000593 int result;
hailfingerb8f7e882008-01-19 00:04:46 +0000594
stuge2bb6ab32008-05-10 23:07:52 +0000595 status = spi_read_status_register();
hailfingerb8f7e882008-01-19 00:04:46 +0000596 /* If there is block protection in effect, unprotect it first. */
597 if ((status & 0x3c) != 0) {
598 printf_debug("Some block protection in effect, disabling\n");
uwe17efbed2008-11-28 21:36:51 +0000599 result = spi_write_status_enable();
hailfingerc1b2e912008-11-18 00:41:02 +0000600 if (result) {
uwe17efbed2008-11-28 21:36:51 +0000601 printf_debug("spi_write_status_enable failed\n");
hailfingerc1b2e912008-11-18 00:41:02 +0000602 return result;
603 }
604 result = spi_write_status_register(status & ~0x3c);
605 if (result) {
606 printf_debug("spi_write_status_register failed\n");
607 return result;
608 }
hailfingerb8f7e882008-01-19 00:04:46 +0000609 }
hailfingerc1b2e912008-11-18 00:41:02 +0000610 return 0;
hailfingerb8f7e882008-01-19 00:04:46 +0000611}
612
hailfingerc1b2e912008-11-18 00:41:02 +0000613int spi_nbyte_read(int address, uint8_t *bytes, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000614{
uwefa98ca12008-10-18 21:14:13 +0000615 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
616 JEDEC_READ,
hailfinger9cd4cf12008-01-22 14:37:31 +0000617 (address >> 16) & 0xff,
618 (address >> 8) & 0xff,
619 (address >> 0) & 0xff,
hailfingerb8f7e882008-01-19 00:04:46 +0000620 };
621
622 /* Send Read */
hailfingerc1b2e912008-11-18 00:41:02 +0000623 return spi_command(sizeof(cmd), len, cmd, bytes);
hailfingerb8f7e882008-01-19 00:04:46 +0000624}
625
stuge2bb6ab32008-05-10 23:07:52 +0000626int spi_chip_read(struct flashchip *flash, uint8_t *buf)
hailfingerb8f7e882008-01-19 00:04:46 +0000627{
stepan3bdf6182008-06-30 23:45:22 +0000628 switch (flashbus) {
629 case BUS_TYPE_IT87XX_SPI:
hailfinger2c361e42008-05-13 23:03:12 +0000630 return it8716f_spi_chip_read(flash, buf);
uwe17efbed2008-11-28 21:36:51 +0000631 case BUS_TYPE_SB600_SPI:
632 return sb600_spi_read(flash, buf);
stepan3bdf6182008-06-30 23:45:22 +0000633 case BUS_TYPE_ICH7_SPI:
634 case BUS_TYPE_ICH9_SPI:
635 case BUS_TYPE_VIA_SPI:
ruik9bc51c02008-06-30 21:38:30 +0000636 return ich_spi_read(flash, buf);
stugea564bcf2009-01-26 03:08:45 +0000637 case BUS_TYPE_WBSIO_SPI:
638 return wbsio_spi_read(flash, buf);
stepan3bdf6182008-06-30 23:45:22 +0000639 default:
uwefa98ca12008-10-18 21:14:13 +0000640 printf_debug
641 ("%s called, but no SPI chipset/strapping detected\n",
642 __FUNCTION__);
stepan3bdf6182008-06-30 23:45:22 +0000643 }
644
hailfinger2c361e42008-05-13 23:03:12 +0000645 return 1;
hailfingerb8f7e882008-01-19 00:04:46 +0000646}
647
hailfingered063f52009-05-09 02:30:21 +0000648/*
649 * Program chip using byte programming. (SLOW!)
650 * This is for chips which can only handle one byte writes
651 * and for chips where memory mapped programming is impossible
652 * (e.g. due to size constraints in IT87* for over 512 kB)
653 */
654int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
655{
656 int total_size = 1024 * flash->total_size;
657 int i;
658
659 spi_disable_blockprotect();
660 for (i = 0; i < total_size; i++) {
661 spi_write_enable();
662 spi_byte_program(i, buf[i]);
663 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
664 myusec_delay(10);
665 }
666
667 return 0;
668}
669
670/*
671 * Program chip using page (256 bytes) programming.
672 * Some SPI masters can't do this, they use single byte programming instead.
673 */
hailfinger87c05482009-05-09 02:34:18 +0000674int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
hailfinger2c361e42008-05-13 23:03:12 +0000675{
stepan3bdf6182008-06-30 23:45:22 +0000676 switch (flashbus) {
677 case BUS_TYPE_IT87XX_SPI:
hailfingered063f52009-05-09 02:30:21 +0000678 return it8716f_spi_chip_write_256(flash, buf);
uwe17efbed2008-11-28 21:36:51 +0000679 case BUS_TYPE_SB600_SPI:
hailfingered063f52009-05-09 02:30:21 +0000680 return sb600_spi_write_1(flash, buf);
stepan3bdf6182008-06-30 23:45:22 +0000681 case BUS_TYPE_ICH7_SPI:
682 case BUS_TYPE_ICH9_SPI:
683 case BUS_TYPE_VIA_SPI:
hailfingered063f52009-05-09 02:30:21 +0000684 return ich_spi_write_256(flash, buf);
stugea564bcf2009-01-26 03:08:45 +0000685 case BUS_TYPE_WBSIO_SPI:
hailfingered063f52009-05-09 02:30:21 +0000686 return wbsio_spi_write_1(flash, buf);
stepan3bdf6182008-06-30 23:45:22 +0000687 default:
uwefa98ca12008-10-18 21:14:13 +0000688 printf_debug
689 ("%s called, but no SPI chipset/strapping detected\n",
690 __FUNCTION__);
stepan3bdf6182008-06-30 23:45:22 +0000691 }
692
hailfinger2c361e42008-05-13 23:03:12 +0000693 return 1;
hailfingerf71c0ac2007-10-18 00:24:07 +0000694}
stuge712ce862009-01-26 03:37:40 +0000695
hailfinger54c14662009-05-13 11:40:08 +0000696uint32_t spi_get_valid_read_addr(void)
697{
698 /* Need to return BBAR for ICH chipsets. */
699 return 0;
700}
701
uwe5e931bc2009-04-15 10:52:49 +0000702int spi_aai_write(struct flashchip *flash, uint8_t *buf)
703{
stuge712ce862009-01-26 03:37:40 +0000704 uint32_t pos = 2, size = flash->total_size * 1024;
705 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
hailfinger61949942009-05-09 02:09:45 +0000706 int result;
707
stuge712ce862009-01-26 03:37:40 +0000708 switch (flashbus) {
uwe5e931bc2009-04-15 10:52:49 +0000709 case BUS_TYPE_WBSIO_SPI:
710 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
711 " degrading to byte program\n", __func__);
hailfingered063f52009-05-09 02:30:21 +0000712 return spi_chip_write_1(flash, buf);
uwe5e931bc2009-04-15 10:52:49 +0000713 default:
714 break;
stuge712ce862009-01-26 03:37:40 +0000715 }
716 flash->erase(flash);
hailfinger61949942009-05-09 02:09:45 +0000717 result = spi_write_enable();
718 if (result)
719 return result;
stuge712ce862009-01-26 03:37:40 +0000720 spi_command(6, 0, w, NULL);
721 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
722 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
723 while (pos < size) {
724 w[1] = buf[pos++];
725 w[2] = buf[pos++];
726 spi_command(3, 0, w, NULL);
727 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
728 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
729 }
730 spi_write_disable();
731 return 0;
732}