blob: 786d555ee9e84632b919feca126f697749905e4e [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
Carl-Daniel Hailfinger70539262007-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"
Carl-Daniel Hailfingerd6cbf762008-05-13 14:58:23 +000029#include "spi.h"
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000030
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +000031
32void spi_prettyprint_status_register(struct flashchip *flash);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000033
Peter Stugefa8c5502008-05-10 23:07:52 +000034int spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000035{
36 if (it8716f_flashport)
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +000037 return it8716f_spi_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +000038 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000039 return 1;
40}
41
Peter Stugefa8c5502008-05-10 23:07:52 +000042static int spi_rdid(unsigned char *readarr)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000043{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +000044 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = {JEDEC_RDID};
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000045
Peter Stugefa8c5502008-05-10 23:07:52 +000046 if (spi_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, cmd, readarr))
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000047 return 1;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +000048 printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1], readarr[2]);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000049 return 0;
50}
51
Peter Stugefa8c5502008-05-10 23:07:52 +000052void spi_write_enable()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000053{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +000054 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = {JEDEC_WREN};
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000055
56 /* Send WREN (Write Enable) */
Peter Stugefa8c5502008-05-10 23:07:52 +000057 spi_command(JEDEC_WREN_OUTSIZE, JEDEC_WREN_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000058}
59
Peter Stugefa8c5502008-05-10 23:07:52 +000060void spi_write_disable()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000061{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +000062 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = {JEDEC_WRDI};
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000063
64 /* Send WRDI (Write Disable) */
Peter Stugefa8c5502008-05-10 23:07:52 +000065 spi_command(JEDEC_WRDI_OUTSIZE, JEDEC_WRDI_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000066}
67
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000068int probe_spi(struct flashchip *flash)
69{
70 unsigned char readarr[3];
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +000071 uint32_t manuf_id;
72 uint32_t model_id;
Peter Stugefa8c5502008-05-10 23:07:52 +000073 if (!spi_rdid(readarr)) {
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +000074 if (!oddparity(readarr[0]))
75 printf_debug("RDID byte 0 parity violation.\n");
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +000076 /* Check if this is a continuation vendor ID */
77 if (readarr[0] == 0x7f) {
Carl-Daniel Hailfingera758f512008-05-14 12:03:06 +000078 if (!oddparity(readarr[1]))
79 printf_debug("RDID byte 1 parity violation.\n");
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +000080 manuf_id = (readarr[0] << 8) | readarr[1];
81 model_id = readarr[2];
82 } else {
83 manuf_id = readarr[0];
84 model_id = (readarr[1] << 8) | readarr[2];
85 }
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000086 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id);
Carl-Daniel Hailfingere973b052008-01-04 16:22:09 +000087 if (manuf_id == flash->manufacture_id &&
88 model_id == flash->model_id) {
89 /* Print the status register to tell the
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +000090 * user about possible write protection.
91 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +000092 spi_prettyprint_status_register(flash);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +000093
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000094 return 1;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +000095 }
Carl-Daniel Hailfingere973b052008-01-04 16:22:09 +000096 /* Test if this is a pure vendor match. */
97 if (manuf_id == flash->manufacture_id &&
98 GENERIC_DEVICE_ID == flash->model_id)
99 return 1;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000100 }
101
102 return 0;
103}
104
Peter Stugefa8c5502008-05-10 23:07:52 +0000105uint8_t spi_read_status_register()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000106{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000107 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = {JEDEC_RDSR};
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000108 unsigned char readarr[1];
109
110 /* Read Status Register */
Peter Stugefa8c5502008-05-10 23:07:52 +0000111 spi_command(JEDEC_RDSR_OUTSIZE, JEDEC_RDSR_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000112 return readarr[0];
113}
114
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000115/* Prettyprint the status register. Common definitions.
116 */
117void spi_prettyprint_status_register_common(uint8_t status)
118{
119 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
120 "%sset\n", (status & (1 << 5)) ? "" : "not ");
121 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
122 "%sset\n", (status & (1 << 4)) ? "" : "not ");
123 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
124 "%sset\n", (status & (1 << 3)) ? "" : "not ");
125 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
126 "%sset\n", (status & (1 << 2)) ? "" : "not ");
127 printf_debug("Chip status register: Write Enable Latch (WEL) is "
128 "%sset\n", (status & (1 << 1)) ? "" : "not ");
129 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
130 "%sset\n", (status & (1 << 0)) ? "" : "not ");
131}
132
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000133/* Prettyprint the status register. Works for
134 * ST M25P series
135 * MX MX25L series
136 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000137void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000138{
139 printf_debug("Chip status register: Status Register Write Disable "
140 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
141 printf_debug("Chip status register: Bit 6 is "
142 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000143 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000144}
145
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000146/* Prettyprint the status register. Works for
147 * SST 25VF016
148 */
149void spi_prettyprint_status_register_sst25vf016(uint8_t status)
150{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000151 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000152 "none",
153 "1F0000H-1FFFFFH",
154 "1E0000H-1FFFFFH",
155 "1C0000H-1FFFFFH",
156 "180000H-1FFFFFH",
157 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000158 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000159 };
160 printf_debug("Chip status register: Block Protect Write Disable "
161 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
162 printf_debug("Chip status register: Auto Address Increment Programming "
163 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
164 spi_prettyprint_status_register_common(status);
165 printf_debug("Resulting block protection : %s\n",
166 bpt[(status & 0x1c) >> 2]);
167}
168
169void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000170{
171 uint8_t status;
172
Peter Stugefa8c5502008-05-10 23:07:52 +0000173 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000174 printf_debug("Chip status register is %02x\n", status);
175 switch (flash->manufacture_id) {
176 case ST_ID:
177 case MX_ID:
178 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000179 spi_prettyprint_status_register_st_m25p(status);
180 break;
181 case SST_ID:
182 if (flash->model_id == SST_25VF016B)
183 spi_prettyprint_status_register_sst25vf016(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000184 break;
185 }
186}
187
Peter Stugefa8c5502008-05-10 23:07:52 +0000188int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000189{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000190 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = {JEDEC_CE_C7};
Carl-Daniel Hailfingerf5df46f2007-12-16 21:15:27 +0000191
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000192 spi_disable_blockprotect();
Peter Stugefa8c5502008-05-10 23:07:52 +0000193 spi_write_enable();
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000194 /* Send CE (Chip Erase) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000195 spi_command(JEDEC_CE_C7_OUTSIZE, JEDEC_CE_C7_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000196 /* Wait until the Write-In-Progress bit is cleared.
197 * This usually takes 1-85 s, so wait in 1 s steps.
198 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000199 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000200 sleep(1);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000201 return 0;
202}
203
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000204/* Block size is usually
205 * 64k for Macronix
206 * 32k for SST
207 * 4-32k non-uniform for EON
208 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000209int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000210{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000211 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = {JEDEC_BE_D8};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000212
213 cmd[1] = (addr & 0x00ff0000) >> 16;
214 cmd[2] = (addr & 0x0000ff00) >> 8;
215 cmd[3] = (addr & 0x000000ff);
Peter Stugefa8c5502008-05-10 23:07:52 +0000216 spi_write_enable();
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000217 /* Send BE (Block Erase) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000218 spi_command(JEDEC_BE_D8_OUTSIZE, JEDEC_BE_D8_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000219 /* Wait until the Write-In-Progress bit is cleared.
220 * This usually takes 100-4000 ms, so wait in 100 ms steps.
221 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000222 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000223 usleep(100 * 1000);
224 return 0;
225}
226
227/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Peter Stugefa8c5502008-05-10 23:07:52 +0000228int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000229{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000230 unsigned char cmd[JEDEC_SE_OUTSIZE] = {JEDEC_SE};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000231 cmd[1] = (addr & 0x00ff0000) >> 16;
232 cmd[2] = (addr & 0x0000ff00) >> 8;
233 cmd[3] = (addr & 0x000000ff);
234
Peter Stugefa8c5502008-05-10 23:07:52 +0000235 spi_write_enable();
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000236 /* Send SE (Sector Erase) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000237 spi_command(JEDEC_SE_OUTSIZE, JEDEC_SE_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000238 /* Wait until the Write-In-Progress bit is cleared.
239 * This usually takes 15-800 ms, so wait in 10 ms steps.
240 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000241 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000242 usleep(10 * 1000);
243 return 0;
244}
245
Peter Stugefa8c5502008-05-10 23:07:52 +0000246void spi_page_program(int block, uint8_t *buf, uint8_t *bios)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000247{
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000248 if (it8716f_flashport) {
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000249 it8716f_spi_page_program(block, buf, bios);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000250 return;
251 }
252 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000253}
254
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000255/*
256 * This is according the SST25VF016 datasheet, who knows it is more
257 * generic that this...
258 */
259void spi_write_status_register(int status)
260{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000261 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] = {JEDEC_WRSR, (unsigned char)status};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000262
263 /* Send WRSR (Write Status Register) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000264 spi_command(JEDEC_WRSR_OUTSIZE, JEDEC_WRSR_INSIZE, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000265}
266
267void spi_byte_program(int address, uint8_t byte)
268{
269 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {JEDEC_BYTE_PROGRAM,
270 (address>>16)&0xff,
271 (address>>8)&0xff,
272 (address>>0)&0xff,
273 byte
274 };
275
276 /* Send Byte-Program */
Peter Stugefa8c5502008-05-10 23:07:52 +0000277 spi_command(JEDEC_BYTE_PROGRAM_OUTSIZE, JEDEC_BYTE_PROGRAM_INSIZE, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000278}
279
280void spi_disable_blockprotect(void)
281{
282 uint8_t status;
283
Peter Stugefa8c5502008-05-10 23:07:52 +0000284 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000285 /* If there is block protection in effect, unprotect it first. */
286 if ((status & 0x3c) != 0) {
287 printf_debug("Some block protection in effect, disabling\n");
Peter Stugefa8c5502008-05-10 23:07:52 +0000288 spi_write_enable();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000289 spi_write_status_register(status & ~0x3c);
290 }
291}
292
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000293void spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000294{
295 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000296 (address >> 16) & 0xff,
297 (address >> 8) & 0xff,
298 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000299 };
300
301 /* Send Read */
Peter Stugefa8c5502008-05-10 23:07:52 +0000302 spi_command(JEDEC_READ_OUTSIZE, len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000303}
304
Peter Stugefa8c5502008-05-10 23:07:52 +0000305int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000306{
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000307 if (it8716f_flashport)
308 return it8716f_spi_chip_read(flash, buf);
309 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
310 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000311}
312
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000313int spi_chip_write(struct flashchip *flash, uint8_t *buf)
314{
315 if (it8716f_flashport)
316 return it8716f_spi_chip_write(flash, buf);
317 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
318 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000319}
320