blob: f50414cbc5832af1bb5d310c8d252749065dedf6 [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
stepand4b13752007-10-15 21:45:29 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 * Contains the generic SPI framework
22 */
23
24#include <stdio.h>
25#include <pci/pci.h>
26#include <stdint.h>
27#include <string.h>
28#include "flash.h"
hailfinger78031562008-05-13 14:58:23 +000029#include "spi.h"
stepand4b13752007-10-15 21:45:29 +000030
hailfingerb8f7e882008-01-19 00:04:46 +000031
32void spi_prettyprint_status_register(struct flashchip *flash);
stepand4b13752007-10-15 21:45:29 +000033
stuge2bb6ab32008-05-10 23:07:52 +000034int spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +000035{
36 if (it8716f_flashport)
hailfinger8d8beb52008-05-10 23:40:51 +000037 return it8716f_spi_command(writecnt, readcnt, writearr, readarr);
hailfingerf1961cb2007-12-29 10:15:58 +000038 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
hailfinger35cc8162007-10-16 21:09:06 +000039 return 1;
40}
41
stuge2bb6ab32008-05-10 23:07:52 +000042static int spi_rdid(unsigned char *readarr)
stepand4b13752007-10-15 21:45:29 +000043{
hailfingereb5dfbd2008-05-13 14:01:22 +000044 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = {JEDEC_RDID};
stepand4b13752007-10-15 21:45:29 +000045
stuge2bb6ab32008-05-10 23:07:52 +000046 if (spi_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, cmd, readarr))
stepand4b13752007-10-15 21:45:29 +000047 return 1;
hailfingerf1961cb2007-12-29 10:15:58 +000048 printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1], readarr[2]);
stepand4b13752007-10-15 21:45:29 +000049 return 0;
50}
51
hailfinger82893122008-05-15 03:19:49 +000052static int spi_res(unsigned char *readarr)
53{
54 const unsigned char cmd[JEDEC_RES_OUTSIZE] = {JEDEC_RES, 0, 0, 0};
55
56 if (spi_command(JEDEC_RES_OUTSIZE, JEDEC_RES_INSIZE, cmd, readarr))
57 return 1;
58 printf_debug("RES returned %02x.\n", readarr[0]);
59 return 0;
60}
61
stuge2bb6ab32008-05-10 23:07:52 +000062void spi_write_enable()
hailfingerf71c0ac2007-10-18 00:24:07 +000063{
hailfingereb5dfbd2008-05-13 14:01:22 +000064 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = {JEDEC_WREN};
hailfingerf71c0ac2007-10-18 00:24:07 +000065
66 /* Send WREN (Write Enable) */
stuge2bb6ab32008-05-10 23:07:52 +000067 spi_command(JEDEC_WREN_OUTSIZE, JEDEC_WREN_INSIZE, cmd, NULL);
hailfingerf71c0ac2007-10-18 00:24:07 +000068}
69
stuge2bb6ab32008-05-10 23:07:52 +000070void spi_write_disable()
hailfingerf71c0ac2007-10-18 00:24:07 +000071{
hailfingereb5dfbd2008-05-13 14:01:22 +000072 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = {JEDEC_WRDI};
hailfingerf71c0ac2007-10-18 00:24:07 +000073
74 /* Send WRDI (Write Disable) */
stuge2bb6ab32008-05-10 23:07:52 +000075 spi_command(JEDEC_WRDI_OUTSIZE, JEDEC_WRDI_INSIZE, cmd, NULL);
hailfingerf71c0ac2007-10-18 00:24:07 +000076}
77
hailfinger82893122008-05-15 03:19:49 +000078int probe_spi_rdid(struct flashchip *flash)
stepand4b13752007-10-15 21:45:29 +000079{
80 unsigned char readarr[3];
hailfinger492e3172008-02-06 22:07:58 +000081 uint32_t manuf_id;
82 uint32_t model_id;
stuge2bb6ab32008-05-10 23:07:52 +000083 if (!spi_rdid(readarr)) {
hailfinger79cf3672008-05-14 12:03:06 +000084 if (!oddparity(readarr[0]))
85 printf_debug("RDID byte 0 parity violation.\n");
hailfinger492e3172008-02-06 22:07:58 +000086 /* Check if this is a continuation vendor ID */
87 if (readarr[0] == 0x7f) {
hailfinger79cf3672008-05-14 12:03:06 +000088 if (!oddparity(readarr[1]))
89 printf_debug("RDID byte 1 parity violation.\n");
hailfinger492e3172008-02-06 22:07:58 +000090 manuf_id = (readarr[0] << 8) | readarr[1];
91 model_id = readarr[2];
92 } else {
93 manuf_id = readarr[0];
94 model_id = (readarr[1] << 8) | readarr[2];
95 }
stepand4b13752007-10-15 21:45:29 +000096 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id);
hailfingerc5036f22008-01-04 16:22:09 +000097 if (manuf_id == flash->manufacture_id &&
98 model_id == flash->model_id) {
99 /* Print the status register to tell the
hailfingerf1961cb2007-12-29 10:15:58 +0000100 * user about possible write protection.
101 */
hailfingerb8f7e882008-01-19 00:04:46 +0000102 spi_prettyprint_status_register(flash);
hailfingerf1961cb2007-12-29 10:15:58 +0000103
stepand4b13752007-10-15 21:45:29 +0000104 return 1;
hailfingerf1961cb2007-12-29 10:15:58 +0000105 }
hailfingerc5036f22008-01-04 16:22:09 +0000106 /* Test if this is a pure vendor match. */
107 if (manuf_id == flash->manufacture_id &&
108 GENERIC_DEVICE_ID == flash->model_id)
109 return 1;
stepand4b13752007-10-15 21:45:29 +0000110 }
111
112 return 0;
113}
114
hailfinger82893122008-05-15 03:19:49 +0000115int probe_spi_res(struct flashchip *flash)
116{
117 unsigned char readarr[3];
118 uint32_t model_id;
119 if (!spi_rdid(readarr)) {
120 /* Check if RDID returns 0xff 0xff 0xff, then we use RES. */
121 if ((readarr[0] != 0xff) || (readarr[1] != 0xff) ||
122 (readarr[2] != 0xff))
123 return 0;
124 } else {
125 /* We couldn't issue RDID, it's pointless to try RES. */
126 return 0;
127 }
128 if (!spi_res(readarr)) {
129 model_id = readarr[0];
130 printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
131 if (model_id == flash->model_id) {
132 /* Print the status register to tell the
133 * user about possible write protection.
134 */
135 spi_prettyprint_status_register(flash);
136
137 return 1;
138 }
139 }
140
141 return 0;
142}
143
stuge2bb6ab32008-05-10 23:07:52 +0000144uint8_t spi_read_status_register()
hailfingerf71c0ac2007-10-18 00:24:07 +0000145{
hailfingereb5dfbd2008-05-13 14:01:22 +0000146 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = {JEDEC_RDSR};
hailfingerf71c0ac2007-10-18 00:24:07 +0000147 unsigned char readarr[1];
148
149 /* Read Status Register */
stuge2bb6ab32008-05-10 23:07:52 +0000150 spi_command(JEDEC_RDSR_OUTSIZE, JEDEC_RDSR_INSIZE, cmd, readarr);
hailfingerf71c0ac2007-10-18 00:24:07 +0000151 return readarr[0];
152}
153
hailfingerb8f7e882008-01-19 00:04:46 +0000154/* Prettyprint the status register. Common definitions.
155 */
156void spi_prettyprint_status_register_common(uint8_t status)
157{
158 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
159 "%sset\n", (status & (1 << 5)) ? "" : "not ");
160 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
161 "%sset\n", (status & (1 << 4)) ? "" : "not ");
162 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
163 "%sset\n", (status & (1 << 3)) ? "" : "not ");
164 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
165 "%sset\n", (status & (1 << 2)) ? "" : "not ");
166 printf_debug("Chip status register: Write Enable Latch (WEL) is "
167 "%sset\n", (status & (1 << 1)) ? "" : "not ");
168 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
169 "%sset\n", (status & (1 << 0)) ? "" : "not ");
170}
171
hailfingerf1961cb2007-12-29 10:15:58 +0000172/* Prettyprint the status register. Works for
173 * ST M25P series
174 * MX MX25L series
175 */
hailfingerb8f7e882008-01-19 00:04:46 +0000176void spi_prettyprint_status_register_st_m25p(uint8_t status)
hailfingerf1961cb2007-12-29 10:15:58 +0000177{
178 printf_debug("Chip status register: Status Register Write Disable "
179 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
180 printf_debug("Chip status register: Bit 6 is "
181 "%sset\n", (status & (1 << 6)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000182 spi_prettyprint_status_register_common(status);
hailfingerf1961cb2007-12-29 10:15:58 +0000183}
184
hailfingerb8f7e882008-01-19 00:04:46 +0000185/* Prettyprint the status register. Works for
186 * SST 25VF016
187 */
188void spi_prettyprint_status_register_sst25vf016(uint8_t status)
189{
hailfinger9cd4cf12008-01-22 14:37:31 +0000190 const char *bpt[] = {
hailfingerb8f7e882008-01-19 00:04:46 +0000191 "none",
192 "1F0000H-1FFFFFH",
193 "1E0000H-1FFFFFH",
194 "1C0000H-1FFFFFH",
195 "180000H-1FFFFFH",
196 "100000H-1FFFFFH",
hailfinger9cd4cf12008-01-22 14:37:31 +0000197 "all", "all"
hailfingerb8f7e882008-01-19 00:04:46 +0000198 };
199 printf_debug("Chip status register: Block Protect Write Disable "
200 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
201 printf_debug("Chip status register: Auto Address Increment Programming "
202 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
203 spi_prettyprint_status_register_common(status);
204 printf_debug("Resulting block protection : %s\n",
205 bpt[(status & 0x1c) >> 2]);
206}
207
208void spi_prettyprint_status_register(struct flashchip *flash)
hailfingerf1961cb2007-12-29 10:15:58 +0000209{
210 uint8_t status;
211
stuge2bb6ab32008-05-10 23:07:52 +0000212 status = spi_read_status_register();
hailfingerf1961cb2007-12-29 10:15:58 +0000213 printf_debug("Chip status register is %02x\n", status);
214 switch (flash->manufacture_id) {
215 case ST_ID:
hailfinger8b869132008-05-15 22:32:08 +0000216 if (((flash->model_id & 0xff00) == 0x2000) ||
217 ((flash->model_id & 0xff00) == 0x2500))
218 spi_prettyprint_status_register_st_m25p(status);
219 break;
hailfingerf1961cb2007-12-29 10:15:58 +0000220 case MX_ID:
221 if ((flash->model_id & 0xff00) == 0x2000)
hailfingerb8f7e882008-01-19 00:04:46 +0000222 spi_prettyprint_status_register_st_m25p(status);
223 break;
224 case SST_ID:
225 if (flash->model_id == SST_25VF016B)
226 spi_prettyprint_status_register_sst25vf016(status);
hailfingerf1961cb2007-12-29 10:15:58 +0000227 break;
228 }
229}
230
stuge2bb6ab32008-05-10 23:07:52 +0000231int spi_chip_erase_c7(struct flashchip *flash)
hailfingerf71c0ac2007-10-18 00:24:07 +0000232{
hailfingereb5dfbd2008-05-13 14:01:22 +0000233 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = {JEDEC_CE_C7};
hailfingera9698562007-12-16 21:15:27 +0000234
hailfingerb8f7e882008-01-19 00:04:46 +0000235 spi_disable_blockprotect();
stuge2bb6ab32008-05-10 23:07:52 +0000236 spi_write_enable();
hailfingerf71c0ac2007-10-18 00:24:07 +0000237 /* Send CE (Chip Erase) */
stuge2bb6ab32008-05-10 23:07:52 +0000238 spi_command(JEDEC_CE_C7_OUTSIZE, JEDEC_CE_C7_INSIZE, cmd, NULL);
hailfinger1b24dbb2007-10-22 16:15:28 +0000239 /* Wait until the Write-In-Progress bit is cleared.
240 * This usually takes 1-85 s, so wait in 1 s steps.
241 */
stuge2bb6ab32008-05-10 23:07:52 +0000242 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingerf71c0ac2007-10-18 00:24:07 +0000243 sleep(1);
hailfingerf71c0ac2007-10-18 00:24:07 +0000244 return 0;
245}
246
hailfinger1b24dbb2007-10-22 16:15:28 +0000247/* Block size is usually
248 * 64k for Macronix
249 * 32k for SST
250 * 4-32k non-uniform for EON
251 */
stuge2bb6ab32008-05-10 23:07:52 +0000252int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
hailfinger1b24dbb2007-10-22 16:15:28 +0000253{
hailfingereb5dfbd2008-05-13 14:01:22 +0000254 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = {JEDEC_BE_D8};
hailfinger1b24dbb2007-10-22 16:15:28 +0000255
256 cmd[1] = (addr & 0x00ff0000) >> 16;
257 cmd[2] = (addr & 0x0000ff00) >> 8;
258 cmd[3] = (addr & 0x000000ff);
stuge2bb6ab32008-05-10 23:07:52 +0000259 spi_write_enable();
hailfinger1b24dbb2007-10-22 16:15:28 +0000260 /* Send BE (Block Erase) */
stuge2bb6ab32008-05-10 23:07:52 +0000261 spi_command(JEDEC_BE_D8_OUTSIZE, JEDEC_BE_D8_INSIZE, cmd, NULL);
hailfinger1b24dbb2007-10-22 16:15:28 +0000262 /* Wait until the Write-In-Progress bit is cleared.
263 * This usually takes 100-4000 ms, so wait in 100 ms steps.
264 */
stuge2bb6ab32008-05-10 23:07:52 +0000265 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfinger1b24dbb2007-10-22 16:15:28 +0000266 usleep(100 * 1000);
267 return 0;
268}
269
270/* Sector size is usually 4k, though Macronix eliteflash has 64k */
stuge2bb6ab32008-05-10 23:07:52 +0000271int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
hailfinger1b24dbb2007-10-22 16:15:28 +0000272{
hailfingereb5dfbd2008-05-13 14:01:22 +0000273 unsigned char cmd[JEDEC_SE_OUTSIZE] = {JEDEC_SE};
hailfinger1b24dbb2007-10-22 16:15:28 +0000274 cmd[1] = (addr & 0x00ff0000) >> 16;
275 cmd[2] = (addr & 0x0000ff00) >> 8;
276 cmd[3] = (addr & 0x000000ff);
277
stuge2bb6ab32008-05-10 23:07:52 +0000278 spi_write_enable();
hailfinger1b24dbb2007-10-22 16:15:28 +0000279 /* Send SE (Sector Erase) */
stuge2bb6ab32008-05-10 23:07:52 +0000280 spi_command(JEDEC_SE_OUTSIZE, JEDEC_SE_INSIZE, cmd, NULL);
hailfinger1b24dbb2007-10-22 16:15:28 +0000281 /* Wait until the Write-In-Progress bit is cleared.
282 * This usually takes 15-800 ms, so wait in 10 ms steps.
283 */
stuge2bb6ab32008-05-10 23:07:52 +0000284 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfinger1b24dbb2007-10-22 16:15:28 +0000285 usleep(10 * 1000);
286 return 0;
287}
288
stuge2bb6ab32008-05-10 23:07:52 +0000289void spi_page_program(int block, uint8_t *buf, uint8_t *bios)
hailfingerf71c0ac2007-10-18 00:24:07 +0000290{
hailfinger2c361e42008-05-13 23:03:12 +0000291 if (it8716f_flashport) {
hailfingerf71c0ac2007-10-18 00:24:07 +0000292 it8716f_spi_page_program(block, buf, bios);
hailfinger2c361e42008-05-13 23:03:12 +0000293 return;
294 }
295 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
hailfingerf71c0ac2007-10-18 00:24:07 +0000296}
297
hailfingerb8f7e882008-01-19 00:04:46 +0000298/*
299 * This is according the SST25VF016 datasheet, who knows it is more
300 * generic that this...
301 */
302void spi_write_status_register(int status)
303{
hailfingereb5dfbd2008-05-13 14:01:22 +0000304 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] = {JEDEC_WRSR, (unsigned char)status};
hailfingerb8f7e882008-01-19 00:04:46 +0000305
306 /* Send WRSR (Write Status Register) */
stuge2bb6ab32008-05-10 23:07:52 +0000307 spi_command(JEDEC_WRSR_OUTSIZE, JEDEC_WRSR_INSIZE, cmd, NULL);
hailfingerb8f7e882008-01-19 00:04:46 +0000308}
309
310void spi_byte_program(int address, uint8_t byte)
311{
312 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {JEDEC_BYTE_PROGRAM,
313 (address>>16)&0xff,
314 (address>>8)&0xff,
315 (address>>0)&0xff,
316 byte
317 };
318
319 /* Send Byte-Program */
stuge2bb6ab32008-05-10 23:07:52 +0000320 spi_command(JEDEC_BYTE_PROGRAM_OUTSIZE, JEDEC_BYTE_PROGRAM_INSIZE, cmd, NULL);
hailfingerb8f7e882008-01-19 00:04:46 +0000321}
322
323void spi_disable_blockprotect(void)
324{
325 uint8_t status;
326
stuge2bb6ab32008-05-10 23:07:52 +0000327 status = spi_read_status_register();
hailfingerb8f7e882008-01-19 00:04:46 +0000328 /* If there is block protection in effect, unprotect it first. */
329 if ((status & 0x3c) != 0) {
330 printf_debug("Some block protection in effect, disabling\n");
stuge2bb6ab32008-05-10 23:07:52 +0000331 spi_write_enable();
hailfingerb8f7e882008-01-19 00:04:46 +0000332 spi_write_status_register(status & ~0x3c);
333 }
334}
335
hailfinger2c361e42008-05-13 23:03:12 +0000336void spi_nbyte_read(int address, uint8_t *bytes, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000337{
338 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {JEDEC_READ,
hailfinger9cd4cf12008-01-22 14:37:31 +0000339 (address >> 16) & 0xff,
340 (address >> 8) & 0xff,
341 (address >> 0) & 0xff,
hailfingerb8f7e882008-01-19 00:04:46 +0000342 };
343
344 /* Send Read */
stuge2bb6ab32008-05-10 23:07:52 +0000345 spi_command(JEDEC_READ_OUTSIZE, len, cmd, bytes);
hailfingerb8f7e882008-01-19 00:04:46 +0000346}
347
stuge2bb6ab32008-05-10 23:07:52 +0000348int spi_chip_read(struct flashchip *flash, uint8_t *buf)
hailfingerb8f7e882008-01-19 00:04:46 +0000349{
hailfinger2c361e42008-05-13 23:03:12 +0000350 if (it8716f_flashport)
351 return it8716f_spi_chip_read(flash, buf);
352 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
353 return 1;
hailfingerb8f7e882008-01-19 00:04:46 +0000354}
355
hailfinger2c361e42008-05-13 23:03:12 +0000356int spi_chip_write(struct flashchip *flash, uint8_t *buf)
357{
358 if (it8716f_flashport)
359 return it8716f_spi_chip_write(flash, buf);
360 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
361 return 1;
hailfingerf71c0ac2007-10-18 00:24:07 +0000362}
363