blob: 756fbe5602a2e35fe03cb740f6d89cb54c3ba50c [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 };
stepand4b13752007-10-15 21:45:29 +000060
stuge494b4eb2008-07-07 06:38:51 +000061 if (spi_command(sizeof(cmd), bytes, cmd, readarr))
stepand4b13752007-10-15 21:45:29 +000062 return 1;
uwefa98ca12008-10-18 21:14:13 +000063 printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1],
64 readarr[2]);
stepand4b13752007-10-15 21:45:29 +000065 return 0;
66}
67
hailfinger3dd0c3e2008-11-28 01:25:00 +000068static int spi_rems(unsigned char *readarr)
69{
70 const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
71
72 if (spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr))
73 return 1;
74 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
75 return 0;
76}
77
hailfinger82893122008-05-15 03:19:49 +000078static int spi_res(unsigned char *readarr)
79{
uwefa98ca12008-10-18 21:14:13 +000080 const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
hailfinger82893122008-05-15 03:19:49 +000081
stuge494b4eb2008-07-07 06:38:51 +000082 if (spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr))
hailfinger82893122008-05-15 03:19:49 +000083 return 1;
84 printf_debug("RES returned %02x.\n", readarr[0]);
85 return 0;
86}
87
hailfingerc1b2e912008-11-18 00:41:02 +000088int spi_write_enable()
hailfingerf71c0ac2007-10-18 00:24:07 +000089{
uwefa98ca12008-10-18 21:14:13 +000090 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
hailfingerf71c0ac2007-10-18 00:24:07 +000091
92 /* Send WREN (Write Enable) */
hailfingerc1b2e912008-11-18 00:41:02 +000093 return spi_command(sizeof(cmd), 0, cmd, NULL);
hailfingerf71c0ac2007-10-18 00:24:07 +000094}
95
hailfingerc1b2e912008-11-18 00:41:02 +000096int spi_write_disable()
hailfingerf71c0ac2007-10-18 00:24:07 +000097{
uwefa98ca12008-10-18 21:14:13 +000098 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
hailfingerf71c0ac2007-10-18 00:24:07 +000099
100 /* Send WRDI (Write Disable) */
hailfingerc1b2e912008-11-18 00:41:02 +0000101 return spi_command(sizeof(cmd), 0, cmd, NULL);
hailfingerf71c0ac2007-10-18 00:24:07 +0000102}
103
ruikdbe18ee2008-06-30 21:45:17 +0000104static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
stepand4b13752007-10-15 21:45:29 +0000105{
ruikdbe18ee2008-06-30 21:45:17 +0000106 unsigned char readarr[4];
hailfinger492e3172008-02-06 22:07:58 +0000107 uint32_t manuf_id;
108 uint32_t model_id;
hailfingerf1961cb2007-12-29 10:15:58 +0000109
ruikdbe18ee2008-06-30 21:45:17 +0000110 if (spi_rdid(readarr, bytes))
stuge7be66832008-06-24 01:22:03 +0000111 return 0;
112
113 if (!oddparity(readarr[0]))
114 printf_debug("RDID byte 0 parity violation.\n");
115
116 /* Check if this is a continuation vendor ID */
117 if (readarr[0] == 0x7f) {
118 if (!oddparity(readarr[1]))
119 printf_debug("RDID byte 1 parity violation.\n");
120 manuf_id = (readarr[0] << 8) | readarr[1];
121 model_id = readarr[2];
ruikdbe18ee2008-06-30 21:45:17 +0000122 if (bytes > 3) {
123 model_id <<= 8;
124 model_id |= readarr[3];
125 }
stuge7be66832008-06-24 01:22:03 +0000126 } else {
127 manuf_id = readarr[0];
128 model_id = (readarr[1] << 8) | readarr[2];
stepand4b13752007-10-15 21:45:29 +0000129 }
130
stugef45dc842009-01-25 23:52:45 +0000131 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, manuf_id,
uwefa98ca12008-10-18 21:14:13 +0000132 model_id);
stuge7be66832008-06-24 01:22:03 +0000133
uwefa98ca12008-10-18 21:14:13 +0000134 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
stuge7be66832008-06-24 01:22:03 +0000135 /* Print the status register to tell the
136 * user about possible write protection.
137 */
138 spi_prettyprint_status_register(flash);
139
140 return 1;
141 }
142
143 /* Test if this is a pure vendor match. */
144 if (manuf_id == flash->manufacture_id &&
145 GENERIC_DEVICE_ID == flash->model_id)
146 return 1;
147
stepand4b13752007-10-15 21:45:29 +0000148 return 0;
149}
150
uwefa98ca12008-10-18 21:14:13 +0000151int probe_spi_rdid(struct flashchip *flash)
152{
ruikdbe18ee2008-06-30 21:45:17 +0000153 return probe_spi_rdid_generic(flash, 3);
154}
155
156/* support 4 bytes flash ID */
uwefa98ca12008-10-18 21:14:13 +0000157int probe_spi_rdid4(struct flashchip *flash)
158{
ruikdbe18ee2008-06-30 21:45:17 +0000159 /* only some SPI chipsets support 4 bytes commands */
stepan3bdf6182008-06-30 23:45:22 +0000160 switch (flashbus) {
161 case BUS_TYPE_ICH7_SPI:
162 case BUS_TYPE_ICH9_SPI:
163 case BUS_TYPE_VIA_SPI:
uwe17efbed2008-11-28 21:36:51 +0000164 case BUS_TYPE_SB600_SPI:
stugea564bcf2009-01-26 03:08:45 +0000165 case BUS_TYPE_WBSIO_SPI:
stepan3bdf6182008-06-30 23:45:22 +0000166 return probe_spi_rdid_generic(flash, 4);
167 default:
168 printf_debug("4b ID not supported on this SPI controller\n");
169 }
170
171 return 0;
ruikdbe18ee2008-06-30 21:45:17 +0000172}
173
hailfinger3dd0c3e2008-11-28 01:25:00 +0000174int probe_spi_rems(struct flashchip *flash)
175{
176 unsigned char readarr[JEDEC_REMS_INSIZE];
177 uint32_t manuf_id, model_id;
178
179 if (spi_rems(readarr))
180 return 0;
181
182 manuf_id = readarr[0];
183 model_id = readarr[1];
184
185 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id,
186 model_id);
187
188 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
189 /* Print the status register to tell the
190 * user about possible write protection.
191 */
192 spi_prettyprint_status_register(flash);
193
194 return 1;
195 }
196
197 /* Test if this is a pure vendor match. */
198 if (manuf_id == flash->manufacture_id &&
199 GENERIC_DEVICE_ID == flash->model_id)
200 return 1;
201
202 return 0;
203}
204
hailfinger82893122008-05-15 03:19:49 +0000205int probe_spi_res(struct flashchip *flash)
206{
207 unsigned char readarr[3];
208 uint32_t model_id;
stuge7be66832008-06-24 01:22:03 +0000209
hailfinger915cc852008-11-27 22:48:48 +0000210 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
211 * In that case, RES is pointless.
212 */
213 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
214 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
stuge7be66832008-06-24 01:22:03 +0000215 return 0;
hailfinger82893122008-05-15 03:19:49 +0000216
stuge7be66832008-06-24 01:22:03 +0000217 if (spi_res(readarr))
218 return 0;
219
220 model_id = readarr[0];
221 printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
222 if (model_id != flash->model_id)
223 return 0;
224
225 /* Print the status register to tell the
226 * user about possible write protection.
227 */
228 spi_prettyprint_status_register(flash);
229 return 1;
hailfinger82893122008-05-15 03:19:49 +0000230}
231
stuge2bb6ab32008-05-10 23:07:52 +0000232uint8_t spi_read_status_register()
hailfingerf71c0ac2007-10-18 00:24:07 +0000233{
uwefa98ca12008-10-18 21:14:13 +0000234 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
stugea564bcf2009-01-26 03:08:45 +0000235 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
hailfingerf71c0ac2007-10-18 00:24:07 +0000236
237 /* Read Status Register */
uwe17efbed2008-11-28 21:36:51 +0000238 if (flashbus == BUS_TYPE_SB600_SPI) {
239 /* SB600 uses a different way to read status register. */
240 return sb600_read_status_register();
241 } else {
242 spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
243 }
244
hailfingerf71c0ac2007-10-18 00:24:07 +0000245 return readarr[0];
246}
247
hailfingerb8f7e882008-01-19 00:04:46 +0000248/* Prettyprint the status register. Common definitions.
249 */
250void spi_prettyprint_status_register_common(uint8_t status)
251{
252 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
uwefa98ca12008-10-18 21:14:13 +0000253 "%sset\n", (status & (1 << 5)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000254 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
uwefa98ca12008-10-18 21:14:13 +0000255 "%sset\n", (status & (1 << 4)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000256 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
uwefa98ca12008-10-18 21:14:13 +0000257 "%sset\n", (status & (1 << 3)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000258 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
uwefa98ca12008-10-18 21:14:13 +0000259 "%sset\n", (status & (1 << 2)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000260 printf_debug("Chip status register: Write Enable Latch (WEL) is "
uwefa98ca12008-10-18 21:14:13 +0000261 "%sset\n", (status & (1 << 1)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000262 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
uwefa98ca12008-10-18 21:14:13 +0000263 "%sset\n", (status & (1 << 0)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000264}
265
hailfingerf1961cb2007-12-29 10:15:58 +0000266/* Prettyprint the status register. Works for
267 * ST M25P series
268 * MX MX25L series
269 */
hailfingerb8f7e882008-01-19 00:04:46 +0000270void spi_prettyprint_status_register_st_m25p(uint8_t status)
hailfingerf1961cb2007-12-29 10:15:58 +0000271{
272 printf_debug("Chip status register: Status Register Write Disable "
uwefa98ca12008-10-18 21:14:13 +0000273 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
hailfingerf1961cb2007-12-29 10:15:58 +0000274 printf_debug("Chip status register: Bit 6 is "
uwefa98ca12008-10-18 21:14:13 +0000275 "%sset\n", (status & (1 << 6)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000276 spi_prettyprint_status_register_common(status);
hailfingerf1961cb2007-12-29 10:15:58 +0000277}
278
hailfingerb8f7e882008-01-19 00:04:46 +0000279/* Prettyprint the status register. Works for
280 * SST 25VF016
281 */
282void spi_prettyprint_status_register_sst25vf016(uint8_t status)
283{
hailfinger9cd4cf12008-01-22 14:37:31 +0000284 const char *bpt[] = {
hailfingerb8f7e882008-01-19 00:04:46 +0000285 "none",
286 "1F0000H-1FFFFFH",
287 "1E0000H-1FFFFFH",
288 "1C0000H-1FFFFFH",
289 "180000H-1FFFFFH",
290 "100000H-1FFFFFH",
hailfinger9cd4cf12008-01-22 14:37:31 +0000291 "all", "all"
hailfingerb8f7e882008-01-19 00:04:46 +0000292 };
293 printf_debug("Chip status register: Block Protect Write Disable "
uwefa98ca12008-10-18 21:14:13 +0000294 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000295 printf_debug("Chip status register: Auto Address Increment Programming "
uwefa98ca12008-10-18 21:14:13 +0000296 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000297 spi_prettyprint_status_register_common(status);
298 printf_debug("Resulting block protection : %s\n",
uwefa98ca12008-10-18 21:14:13 +0000299 bpt[(status & 0x1c) >> 2]);
hailfingerb8f7e882008-01-19 00:04:46 +0000300}
301
stuge36539392009-01-26 03:23:50 +0000302void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
303{
304 const char *bpt[] = {
305 "none",
306 "0x70000-0x7ffff",
307 "0x60000-0x7ffff",
308 "0x40000-0x7ffff",
309 "all blocks", "all blocks", "all blocks", "all blocks"
310 };
311 printf_debug("Chip status register: Block Protect Write Disable "
312 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
313 printf_debug("Chip status register: Auto Address Increment Programming "
314 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
315 spi_prettyprint_status_register_common(status);
316 printf_debug("Resulting block protection : %s\n",
317 bpt[(status & 0x3c) >> 2]);
318}
319
hailfingerb8f7e882008-01-19 00:04:46 +0000320void spi_prettyprint_status_register(struct flashchip *flash)
hailfingerf1961cb2007-12-29 10:15:58 +0000321{
322 uint8_t status;
323
stuge2bb6ab32008-05-10 23:07:52 +0000324 status = spi_read_status_register();
hailfingerf1961cb2007-12-29 10:15:58 +0000325 printf_debug("Chip status register is %02x\n", status);
326 switch (flash->manufacture_id) {
327 case ST_ID:
hailfinger8b869132008-05-15 22:32:08 +0000328 if (((flash->model_id & 0xff00) == 0x2000) ||
329 ((flash->model_id & 0xff00) == 0x2500))
330 spi_prettyprint_status_register_st_m25p(status);
331 break;
hailfingerf1961cb2007-12-29 10:15:58 +0000332 case MX_ID:
333 if ((flash->model_id & 0xff00) == 0x2000)
hailfingerb8f7e882008-01-19 00:04:46 +0000334 spi_prettyprint_status_register_st_m25p(status);
335 break;
336 case SST_ID:
stuge36539392009-01-26 03:23:50 +0000337 switch (flash->model_id) {
338 case 0x2541:
hailfingerb8f7e882008-01-19 00:04:46 +0000339 spi_prettyprint_status_register_sst25vf016(status);
stuge36539392009-01-26 03:23:50 +0000340 break;
341 case 0x8d:
342 case 0x258d:
343 spi_prettyprint_status_register_sst25vf040b(status);
344 break;
345 }
hailfingerf1961cb2007-12-29 10:15:58 +0000346 break;
347 }
348}
uwefa98ca12008-10-18 21:14:13 +0000349
hailfingerffcf81a2008-11-03 00:02:11 +0000350int spi_chip_erase_60(struct flashchip *flash)
351{
352 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
hailfingerc1b2e912008-11-18 00:41:02 +0000353 int result;
hailfingerffcf81a2008-11-03 00:02:11 +0000354
hailfingerc1b2e912008-11-18 00:41:02 +0000355 result = spi_disable_blockprotect();
356 if (result) {
357 printf_debug("spi_disable_blockprotect failed\n");
358 return result;
359 }
360 result = spi_write_enable();
361 if (result) {
362 printf_debug("spi_write_enable failed\n");
363 return result;
364 }
hailfingerffcf81a2008-11-03 00:02:11 +0000365 /* Send CE (Chip Erase) */
hailfingerc1b2e912008-11-18 00:41:02 +0000366 result = spi_command(sizeof(cmd), 0, cmd, NULL);
367 if (result) {
368 printf_debug("spi_chip_erase_60 failed sending erase\n");
369 return result;
370 }
hailfingerffcf81a2008-11-03 00:02:11 +0000371 /* Wait until the Write-In-Progress bit is cleared.
372 * This usually takes 1-85 s, so wait in 1 s steps.
373 */
hailfingerc1b2e912008-11-18 00:41:02 +0000374 /* FIXME: We assume spi_read_status_register will never fail. */
hailfingerffcf81a2008-11-03 00:02:11 +0000375 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
376 sleep(1);
377 return 0;
378}
379
stuge2bb6ab32008-05-10 23:07:52 +0000380int spi_chip_erase_c7(struct flashchip *flash)
hailfingerf71c0ac2007-10-18 00:24:07 +0000381{
uwefa98ca12008-10-18 21:14:13 +0000382 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
hailfingerc1b2e912008-11-18 00:41:02 +0000383 int result;
uwefa98ca12008-10-18 21:14:13 +0000384
hailfingerc1b2e912008-11-18 00:41:02 +0000385 result = spi_disable_blockprotect();
386 if (result) {
387 printf_debug("spi_disable_blockprotect failed\n");
388 return result;
389 }
390 result = spi_write_enable();
391 if (result) {
392 printf_debug("spi_write_enable failed\n");
393 return result;
394 }
hailfingerf71c0ac2007-10-18 00:24:07 +0000395 /* Send CE (Chip Erase) */
hailfingerc1b2e912008-11-18 00:41:02 +0000396 result = spi_command(sizeof(cmd), 0, cmd, NULL);
397 if (result) {
398 printf_debug("spi_chip_erase_60 failed sending erase\n");
399 return result;
400 }
hailfinger1b24dbb2007-10-22 16:15:28 +0000401 /* Wait until the Write-In-Progress bit is cleared.
402 * This usually takes 1-85 s, so wait in 1 s steps.
403 */
hailfingerc1b2e912008-11-18 00:41:02 +0000404 /* FIXME: We assume spi_read_status_register will never fail. */
stuge2bb6ab32008-05-10 23:07:52 +0000405 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingerf71c0ac2007-10-18 00:24:07 +0000406 sleep(1);
hailfingerf71c0ac2007-10-18 00:24:07 +0000407 return 0;
408}
409
hailfingerc1b2e912008-11-18 00:41:02 +0000410int spi_chip_erase_60_c7(struct flashchip *flash)
411{
412 int result;
413 result = spi_chip_erase_60(flash);
414 if (result) {
415 printf_debug("spi_chip_erase_60 failed, trying c7\n");
416 result = spi_chip_erase_c7(flash);
417 }
418 return result;
419}
420
hailfingerffcf81a2008-11-03 00:02:11 +0000421int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
422{
423 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
424
425 cmd[1] = (addr & 0x00ff0000) >> 16;
426 cmd[2] = (addr & 0x0000ff00) >> 8;
427 cmd[3] = (addr & 0x000000ff);
428 spi_write_enable();
429 /* Send BE (Block Erase) */
430 spi_command(sizeof(cmd), 0, cmd, NULL);
431 /* Wait until the Write-In-Progress bit is cleared.
432 * This usually takes 100-4000 ms, so wait in 100 ms steps.
433 */
434 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
435 usleep(100 * 1000);
436 return 0;
437}
438
hailfinger1b24dbb2007-10-22 16:15:28 +0000439/* Block size is usually
440 * 64k for Macronix
441 * 32k for SST
442 * 4-32k non-uniform for EON
443 */
stuge2bb6ab32008-05-10 23:07:52 +0000444int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
hailfinger1b24dbb2007-10-22 16:15:28 +0000445{
uwefa98ca12008-10-18 21:14:13 +0000446 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
hailfinger1b24dbb2007-10-22 16:15:28 +0000447
448 cmd[1] = (addr & 0x00ff0000) >> 16;
449 cmd[2] = (addr & 0x0000ff00) >> 8;
450 cmd[3] = (addr & 0x000000ff);
stuge2bb6ab32008-05-10 23:07:52 +0000451 spi_write_enable();
hailfinger1b24dbb2007-10-22 16:15:28 +0000452 /* Send BE (Block Erase) */
stuge494b4eb2008-07-07 06:38:51 +0000453 spi_command(sizeof(cmd), 0, cmd, NULL);
hailfinger1b24dbb2007-10-22 16:15:28 +0000454 /* Wait until the Write-In-Progress bit is cleared.
455 * This usually takes 100-4000 ms, so wait in 100 ms steps.
456 */
stuge2bb6ab32008-05-10 23:07:52 +0000457 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfinger1b24dbb2007-10-22 16:15:28 +0000458 usleep(100 * 1000);
459 return 0;
460}
461
stepan0f7bff02008-10-29 22:13:20 +0000462int spi_chip_erase_d8(struct flashchip *flash)
463{
464 int i, rc = 0;
465 int total_size = flash->total_size * 1024;
466 int erase_size = 64 * 1024;
467
468 spi_disable_blockprotect();
469
470 printf("Erasing chip: \n");
471
472 for (i = 0; i < total_size / erase_size; i++) {
473 rc = spi_block_erase_d8(flash, i * erase_size);
474 if (rc) {
475 printf("Error erasing block at 0x%x\n", i);
476 break;
477 }
478 }
479
480 printf("\n");
481
482 return rc;
483}
484
hailfinger1b24dbb2007-10-22 16:15:28 +0000485/* Sector size is usually 4k, though Macronix eliteflash has 64k */
stuge2bb6ab32008-05-10 23:07:52 +0000486int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
hailfinger1b24dbb2007-10-22 16:15:28 +0000487{
uwefa98ca12008-10-18 21:14:13 +0000488 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
hailfinger1b24dbb2007-10-22 16:15:28 +0000489 cmd[1] = (addr & 0x00ff0000) >> 16;
490 cmd[2] = (addr & 0x0000ff00) >> 8;
491 cmd[3] = (addr & 0x000000ff);
492
stuge2bb6ab32008-05-10 23:07:52 +0000493 spi_write_enable();
hailfinger1b24dbb2007-10-22 16:15:28 +0000494 /* Send SE (Sector Erase) */
stuge494b4eb2008-07-07 06:38:51 +0000495 spi_command(sizeof(cmd), 0, cmd, NULL);
hailfinger1b24dbb2007-10-22 16:15:28 +0000496 /* Wait until the Write-In-Progress bit is cleared.
497 * This usually takes 15-800 ms, so wait in 10 ms steps.
498 */
stuge2bb6ab32008-05-10 23:07:52 +0000499 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfinger1b24dbb2007-10-22 16:15:28 +0000500 usleep(10 * 1000);
501 return 0;
502}
503
uwe17efbed2008-11-28 21:36:51 +0000504int spi_write_status_enable()
505{
506 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
507
508 /* Send EWSR (Enable Write Status Register). */
509 return spi_command(JEDEC_EWSR_OUTSIZE, JEDEC_EWSR_INSIZE, cmd, NULL);
510}
511
hailfingerb8f7e882008-01-19 00:04:46 +0000512/*
513 * This is according the SST25VF016 datasheet, who knows it is more
514 * generic that this...
515 */
hailfingerc1b2e912008-11-18 00:41:02 +0000516int spi_write_status_register(int status)
hailfingerb8f7e882008-01-19 00:04:46 +0000517{
uwefa98ca12008-10-18 21:14:13 +0000518 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
519 { JEDEC_WRSR, (unsigned char)status };
hailfingerb8f7e882008-01-19 00:04:46 +0000520
521 /* Send WRSR (Write Status Register) */
hailfingerc1b2e912008-11-18 00:41:02 +0000522 return spi_command(sizeof(cmd), 0, cmd, NULL);
hailfingerb8f7e882008-01-19 00:04:46 +0000523}
524
525void spi_byte_program(int address, uint8_t byte)
526{
uwefa98ca12008-10-18 21:14:13 +0000527 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
528 JEDEC_BYTE_PROGRAM,
529 (address >> 16) & 0xff,
530 (address >> 8) & 0xff,
531 (address >> 0) & 0xff,
hailfingerb8f7e882008-01-19 00:04:46 +0000532 byte
533 };
534
535 /* Send Byte-Program */
stuge494b4eb2008-07-07 06:38:51 +0000536 spi_command(sizeof(cmd), 0, cmd, NULL);
hailfingerb8f7e882008-01-19 00:04:46 +0000537}
538
hailfingerc1b2e912008-11-18 00:41:02 +0000539int spi_disable_blockprotect(void)
hailfingerb8f7e882008-01-19 00:04:46 +0000540{
541 uint8_t status;
hailfingerc1b2e912008-11-18 00:41:02 +0000542 int result;
hailfingerb8f7e882008-01-19 00:04:46 +0000543
stuge2bb6ab32008-05-10 23:07:52 +0000544 status = spi_read_status_register();
hailfingerb8f7e882008-01-19 00:04:46 +0000545 /* If there is block protection in effect, unprotect it first. */
546 if ((status & 0x3c) != 0) {
547 printf_debug("Some block protection in effect, disabling\n");
uwe17efbed2008-11-28 21:36:51 +0000548 result = spi_write_status_enable();
hailfingerc1b2e912008-11-18 00:41:02 +0000549 if (result) {
uwe17efbed2008-11-28 21:36:51 +0000550 printf_debug("spi_write_status_enable failed\n");
hailfingerc1b2e912008-11-18 00:41:02 +0000551 return result;
552 }
553 result = spi_write_status_register(status & ~0x3c);
554 if (result) {
555 printf_debug("spi_write_status_register failed\n");
556 return result;
557 }
hailfingerb8f7e882008-01-19 00:04:46 +0000558 }
hailfingerc1b2e912008-11-18 00:41:02 +0000559 return 0;
hailfingerb8f7e882008-01-19 00:04:46 +0000560}
561
hailfingerc1b2e912008-11-18 00:41:02 +0000562int spi_nbyte_read(int address, uint8_t *bytes, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000563{
uwefa98ca12008-10-18 21:14:13 +0000564 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
565 JEDEC_READ,
hailfinger9cd4cf12008-01-22 14:37:31 +0000566 (address >> 16) & 0xff,
567 (address >> 8) & 0xff,
568 (address >> 0) & 0xff,
hailfingerb8f7e882008-01-19 00:04:46 +0000569 };
570
571 /* Send Read */
hailfingerc1b2e912008-11-18 00:41:02 +0000572 return spi_command(sizeof(cmd), len, cmd, bytes);
hailfingerb8f7e882008-01-19 00:04:46 +0000573}
574
stuge2bb6ab32008-05-10 23:07:52 +0000575int spi_chip_read(struct flashchip *flash, uint8_t *buf)
hailfingerb8f7e882008-01-19 00:04:46 +0000576{
stepan3bdf6182008-06-30 23:45:22 +0000577 switch (flashbus) {
578 case BUS_TYPE_IT87XX_SPI:
hailfinger2c361e42008-05-13 23:03:12 +0000579 return it8716f_spi_chip_read(flash, buf);
uwe17efbed2008-11-28 21:36:51 +0000580 case BUS_TYPE_SB600_SPI:
581 return sb600_spi_read(flash, buf);
stepan3bdf6182008-06-30 23:45:22 +0000582 case BUS_TYPE_ICH7_SPI:
583 case BUS_TYPE_ICH9_SPI:
584 case BUS_TYPE_VIA_SPI:
ruik9bc51c02008-06-30 21:38:30 +0000585 return ich_spi_read(flash, buf);
stugea564bcf2009-01-26 03:08:45 +0000586 case BUS_TYPE_WBSIO_SPI:
587 return wbsio_spi_read(flash, buf);
stepan3bdf6182008-06-30 23:45:22 +0000588 default:
uwefa98ca12008-10-18 21:14:13 +0000589 printf_debug
590 ("%s called, but no SPI chipset/strapping detected\n",
591 __FUNCTION__);
stepan3bdf6182008-06-30 23:45:22 +0000592 }
593
hailfinger2c361e42008-05-13 23:03:12 +0000594 return 1;
hailfingerb8f7e882008-01-19 00:04:46 +0000595}
596
hailfinger2c361e42008-05-13 23:03:12 +0000597int spi_chip_write(struct flashchip *flash, uint8_t *buf)
598{
stepan3bdf6182008-06-30 23:45:22 +0000599 switch (flashbus) {
600 case BUS_TYPE_IT87XX_SPI:
hailfinger2c361e42008-05-13 23:03:12 +0000601 return it8716f_spi_chip_write(flash, buf);
uwe17efbed2008-11-28 21:36:51 +0000602 case BUS_TYPE_SB600_SPI:
603 return sb600_spi_write(flash, buf);
stepan3bdf6182008-06-30 23:45:22 +0000604 case BUS_TYPE_ICH7_SPI:
605 case BUS_TYPE_ICH9_SPI:
606 case BUS_TYPE_VIA_SPI:
ruik9bc51c02008-06-30 21:38:30 +0000607 return ich_spi_write(flash, buf);
stugea564bcf2009-01-26 03:08:45 +0000608 case BUS_TYPE_WBSIO_SPI:
609 return wbsio_spi_write(flash, buf);
stepan3bdf6182008-06-30 23:45:22 +0000610 default:
uwefa98ca12008-10-18 21:14:13 +0000611 printf_debug
612 ("%s called, but no SPI chipset/strapping detected\n",
613 __FUNCTION__);
stepan3bdf6182008-06-30 23:45:22 +0000614 }
615
hailfinger2c361e42008-05-13 23:03:12 +0000616 return 1;
hailfingerf71c0ac2007-10-18 00:24:07 +0000617}