blob: f6768571da52042719c78a6918639dd258d4f689 [file] [log] [blame]
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +00004 * Copyright (C) 2007, 2008 Carl-Daniel Hailfinger
Stefan Reinauera9424d52008-06-27 16:28:34 +00005 * Copyright (C) 2008 coresystems GmbH
Carl-Daniel Hailfinger70539262007-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"
Carl-Daniel Hailfingerd6cbf762008-05-13 14:58:23 +000030#include "spi.h"
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000031
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +000032void spi_prettyprint_status_register(struct flashchip *flash);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000033
Uwe Hermann394131e2008-10-18 21:14:13 +000034int spi_command(unsigned int writecnt, unsigned int readcnt,
35 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000036{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000037 switch (flashbus) {
38 case BUS_TYPE_IT87XX_SPI:
Uwe Hermann394131e2008-10-18 21:14:13 +000039 return it8716f_spi_command(writecnt, readcnt, writearr,
40 readarr);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000041 case BUS_TYPE_ICH7_SPI:
42 case BUS_TYPE_ICH9_SPI:
43 case BUS_TYPE_VIA_SPI:
Uwe Hermann394131e2008-10-18 21:14:13 +000044 return ich_spi_command(writecnt, readcnt, writearr, readarr);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000045 default:
Uwe Hermann394131e2008-10-18 21:14:13 +000046 printf_debug
47 ("%s called, but no SPI chipset/strapping detected\n",
48 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000049 }
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000050 return 1;
51}
52
Rudolf Marek48a85e42008-06-30 21:45:17 +000053static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000054{
Uwe Hermann394131e2008-10-18 21:14:13 +000055 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000056
Peter Stugef83221b2008-07-07 06:38:51 +000057 if (spi_command(sizeof(cmd), bytes, cmd, readarr))
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000058 return 1;
Uwe Hermann394131e2008-10-18 21:14:13 +000059 printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1],
60 readarr[2]);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000061 return 0;
62}
63
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000064static int spi_res(unsigned char *readarr)
65{
Uwe Hermann394131e2008-10-18 21:14:13 +000066 const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000067
Peter Stugef83221b2008-07-07 06:38:51 +000068 if (spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr))
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000069 return 1;
70 printf_debug("RES returned %02x.\n", readarr[0]);
71 return 0;
72}
73
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +000074int spi_write_enable()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000075{
Uwe Hermann394131e2008-10-18 21:14:13 +000076 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000077
78 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +000079 return spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000080}
81
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +000082int spi_write_disable()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000083{
Uwe Hermann394131e2008-10-18 21:14:13 +000084 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000085
86 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +000087 return spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000088}
89
Rudolf Marek48a85e42008-06-30 21:45:17 +000090static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000091{
Rudolf Marek48a85e42008-06-30 21:45:17 +000092 unsigned char readarr[4];
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +000093 uint32_t manuf_id;
94 uint32_t model_id;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +000095
Rudolf Marek48a85e42008-06-30 21:45:17 +000096 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +000097 return 0;
98
99 if (!oddparity(readarr[0]))
100 printf_debug("RDID byte 0 parity violation.\n");
101
102 /* Check if this is a continuation vendor ID */
103 if (readarr[0] == 0x7f) {
104 if (!oddparity(readarr[1]))
105 printf_debug("RDID byte 1 parity violation.\n");
106 manuf_id = (readarr[0] << 8) | readarr[1];
107 model_id = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000108 if (bytes > 3) {
109 model_id <<= 8;
110 model_id |= readarr[3];
111 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000112 } else {
113 manuf_id = readarr[0];
114 model_id = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000115 }
116
Uwe Hermann394131e2008-10-18 21:14:13 +0000117 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id,
118 model_id);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000119
Uwe Hermann394131e2008-10-18 21:14:13 +0000120 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000121 /* Print the status register to tell the
122 * user about possible write protection.
123 */
124 spi_prettyprint_status_register(flash);
125
126 return 1;
127 }
128
129 /* Test if this is a pure vendor match. */
130 if (manuf_id == flash->manufacture_id &&
131 GENERIC_DEVICE_ID == flash->model_id)
132 return 1;
133
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000134 return 0;
135}
136
Uwe Hermann394131e2008-10-18 21:14:13 +0000137int probe_spi_rdid(struct flashchip *flash)
138{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000139 return probe_spi_rdid_generic(flash, 3);
140}
141
142/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000143int probe_spi_rdid4(struct flashchip *flash)
144{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000145 /* only some SPI chipsets support 4 bytes commands */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000146 switch (flashbus) {
147 case BUS_TYPE_ICH7_SPI:
148 case BUS_TYPE_ICH9_SPI:
149 case BUS_TYPE_VIA_SPI:
150 return probe_spi_rdid_generic(flash, 4);
151 default:
152 printf_debug("4b ID not supported on this SPI controller\n");
153 }
154
155 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000156}
157
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000158int probe_spi_res(struct flashchip *flash)
159{
160 unsigned char readarr[3];
161 uint32_t model_id;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000162
Rudolf Marek48a85e42008-06-30 21:45:17 +0000163 if (spi_rdid(readarr, 3))
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000164 /* We couldn't issue RDID, it's pointless to try RES. */
165 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000166
Peter Stugeda4e5f32008-06-24 01:22:03 +0000167 /* Check if RDID returns 0xff 0xff 0xff, then we use RES. */
168 if ((readarr[0] != 0xff) || (readarr[1] != 0xff) ||
169 (readarr[2] != 0xff))
170 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000171
Peter Stugeda4e5f32008-06-24 01:22:03 +0000172 if (spi_res(readarr))
173 return 0;
174
175 model_id = readarr[0];
176 printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
177 if (model_id != flash->model_id)
178 return 0;
179
180 /* Print the status register to tell the
181 * user about possible write protection.
182 */
183 spi_prettyprint_status_register(flash);
184 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000185}
186
Peter Stugefa8c5502008-05-10 23:07:52 +0000187uint8_t spi_read_status_register()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000188{
Uwe Hermann394131e2008-10-18 21:14:13 +0000189 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Peter Stugef83221b2008-07-07 06:38:51 +0000190 unsigned char readarr[JEDEC_RDSR_INSIZE];
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000191
192 /* Read Status Register */
Peter Stugef83221b2008-07-07 06:38:51 +0000193 spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000194 return readarr[0];
195}
196
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000197/* Prettyprint the status register. Common definitions.
198 */
199void spi_prettyprint_status_register_common(uint8_t status)
200{
201 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000202 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000203 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000204 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000205 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000206 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000207 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000208 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000209 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000210 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000211 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000212 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000213}
214
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000215/* Prettyprint the status register. Works for
216 * ST M25P series
217 * MX MX25L series
218 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000219void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000220{
221 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000222 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000223 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000224 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000225 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000226}
227
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000228/* Prettyprint the status register. Works for
229 * SST 25VF016
230 */
231void spi_prettyprint_status_register_sst25vf016(uint8_t status)
232{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000233 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000234 "none",
235 "1F0000H-1FFFFFH",
236 "1E0000H-1FFFFFH",
237 "1C0000H-1FFFFFH",
238 "180000H-1FFFFFH",
239 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000240 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000241 };
242 printf_debug("Chip status register: Block Protect Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000243 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000244 printf_debug("Chip status register: Auto Address Increment Programming "
Uwe Hermann394131e2008-10-18 21:14:13 +0000245 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000246 spi_prettyprint_status_register_common(status);
247 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000248 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000249}
250
251void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000252{
253 uint8_t status;
254
Peter Stugefa8c5502008-05-10 23:07:52 +0000255 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000256 printf_debug("Chip status register is %02x\n", status);
257 switch (flash->manufacture_id) {
258 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000259 if (((flash->model_id & 0xff00) == 0x2000) ||
260 ((flash->model_id & 0xff00) == 0x2500))
261 spi_prettyprint_status_register_st_m25p(status);
262 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000263 case MX_ID:
264 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000265 spi_prettyprint_status_register_st_m25p(status);
266 break;
267 case SST_ID:
268 if (flash->model_id == SST_25VF016B)
269 spi_prettyprint_status_register_sst25vf016(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000270 break;
271 }
272}
Uwe Hermann394131e2008-10-18 21:14:13 +0000273
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000274int spi_chip_erase_60(struct flashchip *flash)
275{
276 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000277 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000278
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000279 result = spi_disable_blockprotect();
280 if (result) {
281 printf_debug("spi_disable_blockprotect failed\n");
282 return result;
283 }
284 result = spi_write_enable();
285 if (result) {
286 printf_debug("spi_write_enable failed\n");
287 return result;
288 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000289 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000290 result = spi_command(sizeof(cmd), 0, cmd, NULL);
291 if (result) {
292 printf_debug("spi_chip_erase_60 failed sending erase\n");
293 return result;
294 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000295 /* Wait until the Write-In-Progress bit is cleared.
296 * This usually takes 1-85 s, so wait in 1 s steps.
297 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000298 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000299 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
300 sleep(1);
301 return 0;
302}
303
Peter Stugefa8c5502008-05-10 23:07:52 +0000304int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000305{
Uwe Hermann394131e2008-10-18 21:14:13 +0000306 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000307 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000308
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000309 result = spi_disable_blockprotect();
310 if (result) {
311 printf_debug("spi_disable_blockprotect failed\n");
312 return result;
313 }
314 result = spi_write_enable();
315 if (result) {
316 printf_debug("spi_write_enable failed\n");
317 return result;
318 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000319 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000320 result = spi_command(sizeof(cmd), 0, cmd, NULL);
321 if (result) {
322 printf_debug("spi_chip_erase_60 failed sending erase\n");
323 return result;
324 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000325 /* Wait until the Write-In-Progress bit is cleared.
326 * This usually takes 1-85 s, so wait in 1 s steps.
327 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000328 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000329 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000330 sleep(1);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000331 return 0;
332}
333
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000334int spi_chip_erase_60_c7(struct flashchip *flash)
335{
336 int result;
337 result = spi_chip_erase_60(flash);
338 if (result) {
339 printf_debug("spi_chip_erase_60 failed, trying c7\n");
340 result = spi_chip_erase_c7(flash);
341 }
342 return result;
343}
344
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000345int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
346{
347 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
348
349 cmd[1] = (addr & 0x00ff0000) >> 16;
350 cmd[2] = (addr & 0x0000ff00) >> 8;
351 cmd[3] = (addr & 0x000000ff);
352 spi_write_enable();
353 /* Send BE (Block Erase) */
354 spi_command(sizeof(cmd), 0, cmd, NULL);
355 /* Wait until the Write-In-Progress bit is cleared.
356 * This usually takes 100-4000 ms, so wait in 100 ms steps.
357 */
358 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
359 usleep(100 * 1000);
360 return 0;
361}
362
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000363/* Block size is usually
364 * 64k for Macronix
365 * 32k for SST
366 * 4-32k non-uniform for EON
367 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000368int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000369{
Uwe Hermann394131e2008-10-18 21:14:13 +0000370 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000371
372 cmd[1] = (addr & 0x00ff0000) >> 16;
373 cmd[2] = (addr & 0x0000ff00) >> 8;
374 cmd[3] = (addr & 0x000000ff);
Peter Stugefa8c5502008-05-10 23:07:52 +0000375 spi_write_enable();
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000376 /* Send BE (Block Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000377 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000378 /* Wait until the Write-In-Progress bit is cleared.
379 * This usually takes 100-4000 ms, so wait in 100 ms steps.
380 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000381 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000382 usleep(100 * 1000);
383 return 0;
384}
385
Stefan Reinauer424ed222008-10-29 22:13:20 +0000386int spi_chip_erase_d8(struct flashchip *flash)
387{
388 int i, rc = 0;
389 int total_size = flash->total_size * 1024;
390 int erase_size = 64 * 1024;
391
392 spi_disable_blockprotect();
393
394 printf("Erasing chip: \n");
395
396 for (i = 0; i < total_size / erase_size; i++) {
397 rc = spi_block_erase_d8(flash, i * erase_size);
398 if (rc) {
399 printf("Error erasing block at 0x%x\n", i);
400 break;
401 }
402 }
403
404 printf("\n");
405
406 return rc;
407}
408
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000409/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Peter Stugefa8c5502008-05-10 23:07:52 +0000410int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000411{
Uwe Hermann394131e2008-10-18 21:14:13 +0000412 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000413 cmd[1] = (addr & 0x00ff0000) >> 16;
414 cmd[2] = (addr & 0x0000ff00) >> 8;
415 cmd[3] = (addr & 0x000000ff);
416
Peter Stugefa8c5502008-05-10 23:07:52 +0000417 spi_write_enable();
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000418 /* Send SE (Sector Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000419 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000420 /* Wait until the Write-In-Progress bit is cleared.
421 * This usually takes 15-800 ms, so wait in 10 ms steps.
422 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000423 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000424 usleep(10 * 1000);
425 return 0;
426}
427
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000428/*
429 * This is according the SST25VF016 datasheet, who knows it is more
430 * generic that this...
431 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000432int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000433{
Uwe Hermann394131e2008-10-18 21:14:13 +0000434 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
435 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000436
437 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000438 return spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000439}
440
441void spi_byte_program(int address, uint8_t byte)
442{
Uwe Hermann394131e2008-10-18 21:14:13 +0000443 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
444 JEDEC_BYTE_PROGRAM,
445 (address >> 16) & 0xff,
446 (address >> 8) & 0xff,
447 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000448 byte
449 };
450
451 /* Send Byte-Program */
Peter Stugef83221b2008-07-07 06:38:51 +0000452 spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000453}
454
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000455int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000456{
457 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000458 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000459
Peter Stugefa8c5502008-05-10 23:07:52 +0000460 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000461 /* If there is block protection in effect, unprotect it first. */
462 if ((status & 0x3c) != 0) {
463 printf_debug("Some block protection in effect, disabling\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000464 result = spi_write_enable();
465 if (result) {
466 printf_debug("spi_write_enable failed\n");
467 return result;
468 }
469 result = spi_write_status_register(status & ~0x3c);
470 if (result) {
471 printf_debug("spi_write_status_register failed\n");
472 return result;
473 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000474 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000475 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000476}
477
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000478int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000479{
Uwe Hermann394131e2008-10-18 21:14:13 +0000480 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
481 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000482 (address >> 16) & 0xff,
483 (address >> 8) & 0xff,
484 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000485 };
486
487 /* Send Read */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000488 return spi_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000489}
490
Peter Stugefa8c5502008-05-10 23:07:52 +0000491int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000492{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000493 switch (flashbus) {
494 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000495 return it8716f_spi_chip_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000496 case BUS_TYPE_ICH7_SPI:
497 case BUS_TYPE_ICH9_SPI:
498 case BUS_TYPE_VIA_SPI:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000499 return ich_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000500 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000501 printf_debug
502 ("%s called, but no SPI chipset/strapping detected\n",
503 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000504 }
505
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000506 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000507}
508
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000509int spi_chip_write(struct flashchip *flash, uint8_t *buf)
510{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000511 switch (flashbus) {
512 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000513 return it8716f_spi_chip_write(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000514 case BUS_TYPE_ICH7_SPI:
515 case BUS_TYPE_ICH9_SPI:
516 case BUS_TYPE_VIA_SPI:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000517 return ich_spi_write(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000518 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000519 printf_debug
520 ("%s called, but no SPI chipset/strapping detected\n",
521 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000522 }
523
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000524 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000525}