blob: cba69422098ab23ddaa4016d7d78ae68e717c00b [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
5 * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl>
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
32#define ITE_SUPERIO_PORT1 0x2e
33#define ITE_SUPERIO_PORT2 0x4e
34
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +000035
36uint16_t it8716f_flashport = 0;
37/* use fast 33MHz SPI (<>0) or slow 16MHz (0) */
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +000038int fast_spi = 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +000039
40void spi_prettyprint_status_register(struct flashchip *flash);
41void spi_disable_blockprotect(void);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +000042
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000043/* Generic Super I/O helper functions */
44uint8_t regval(uint16_t port, uint8_t reg)
45{
46 outb(reg, port);
47 return inb(port + 1);
48}
49
50void regwrite(uint16_t port, uint8_t reg, uint8_t val)
51{
52 outb(reg, port);
53 outb(val, port + 1);
54}
55
56/* Helper functions for most recent ITE IT87xx Super I/O chips */
57#define CHIP_ID_BYTE1_REG 0x20
58#define CHIP_ID_BYTE2_REG 0x21
59static void enter_conf_mode_ite(uint16_t port)
60{
61 outb(0x87, port);
62 outb(0x01, port);
63 outb(0x55, port);
64 if (port == ITE_SUPERIO_PORT1)
65 outb(0x55, port);
66 else
67 outb(0xaa, port);
68}
69
70static void exit_conf_mode_ite(uint16_t port)
71{
72 regwrite(port, 0x02, 0x02);
73}
74
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000075static uint16_t find_ite_spi_flash_port(uint16_t port)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000076{
77 uint8_t tmp = 0;
78 uint16_t id, flashport = 0;
79
80 enter_conf_mode_ite(port);
81
82 id = regval(port, CHIP_ID_BYTE1_REG) << 8;
83 id |= regval(port, CHIP_ID_BYTE2_REG);
84
85 /* TODO: Handle more IT87xx if they support flash translation */
86 if (id == 0x8716) {
87 /* NOLDN, reg 0x24, mask out lowest bit (suspend) */
88 tmp = regval(port, 0x24) & 0xFE;
89 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
90 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis");
91 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
92 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis");
93 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
94 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis");
95 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
96 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis");
97 printf("LPC write to serial flash %sabled\n",
98 (tmp & 1 << 4) ? "en" : "dis");
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +000099 printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000100 /* LDN 0x7, reg 0x64/0x65 */
101 regwrite(port, 0x07, 0x7);
102 flashport = regval(port, 0x64) << 8;
103 flashport |= regval(port, 0x65);
104 }
105 exit_conf_mode_ite(port);
106 return flashport;
107}
108
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000109int it87xx_probe_spi_flash(const char *name)
110{
111 it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1);
112 if (!it8716f_flashport)
113 it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2);
114 return (!it8716f_flashport);
115}
116
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000117/* The IT8716F only supports commands with length 1,2,4,5 bytes including
118 command byte and can not read more than 3 bytes from the device.
119 This function expects writearr[0] to be the first byte sent to the device,
120 whereas the IT8716F splits commands internally into address and non-address
121 commands with the address in inverse wire order. That's why the register
122 ordering in case 4 and 5 may seem strange. */
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000123static int it8716f_spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000124{
125 uint8_t busy, writeenc;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000126 int i;
127
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000128 do {
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000129 busy = inb(it8716f_flashport) & 0x80;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000130 } while (busy);
131 if (readcnt > 3) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000132 printf("%s called with unsupported readcnt %i.\n",
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000133 __FUNCTION__, readcnt);
134 return 1;
135 }
136 switch (writecnt) {
137 case 1:
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000138 outb(writearr[0], it8716f_flashport + 1);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000139 writeenc = 0x0;
140 break;
141 case 2:
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000142 outb(writearr[0], it8716f_flashport + 1);
143 outb(writearr[1], it8716f_flashport + 7);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000144 writeenc = 0x1;
145 break;
146 case 4:
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000147 outb(writearr[0], it8716f_flashport + 1);
148 outb(writearr[1], it8716f_flashport + 4);
149 outb(writearr[2], it8716f_flashport + 3);
150 outb(writearr[3], it8716f_flashport + 2);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000151 writeenc = 0x2;
152 break;
153 case 5:
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000154 outb(writearr[0], it8716f_flashport + 1);
155 outb(writearr[1], it8716f_flashport + 4);
156 outb(writearr[2], it8716f_flashport + 3);
157 outb(writearr[3], it8716f_flashport + 2);
158 outb(writearr[4], it8716f_flashport + 7);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000159 writeenc = 0x3;
160 break;
161 default:
Uwe Hermanna502dce2007-10-17 23:55:15 +0000162 printf("%s called with unsupported writecnt %i.\n",
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000163 __FUNCTION__, writecnt);
164 return 1;
165 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000166 /* Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes.
167 * Note:
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000168 * We can't use writecnt directly, but have to use a strange encoding.
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000169 */
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000170 outb(((0x4 + (fast_spi ? 1 : 0)) << 4) | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000171
Ronald Hoogenboomd4554c52008-01-21 23:55:08 +0000172 if (readcnt > 0) {
173 do {
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000174 busy = inb(it8716f_flashport) & 0x80;
Ronald Hoogenboomd4554c52008-01-21 23:55:08 +0000175 } while (busy);
176
177 for (i = 0; i < readcnt; i++) {
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000178 readarr[i] = inb(it8716f_flashport + 5 + i);
Ronald Hoogenboomd4554c52008-01-21 23:55:08 +0000179 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000180 }
181
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000182 return 0;
183}
184
Peter Stugefa8c5502008-05-10 23:07:52 +0000185int spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000186{
187 if (it8716f_flashport)
Carl-Daniel Hailfingera5b8efd2008-05-10 23:40:51 +0000188 return it8716f_spi_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000189 printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000190 return 1;
191}
192
Peter Stugefa8c5502008-05-10 23:07:52 +0000193static int spi_rdid(unsigned char *readarr)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000194{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000195 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = {JEDEC_RDID};
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000196
Peter Stugefa8c5502008-05-10 23:07:52 +0000197 if (spi_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, cmd, readarr))
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000198 return 1;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000199 printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1], readarr[2]);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000200 return 0;
201}
202
Peter Stugefa8c5502008-05-10 23:07:52 +0000203void spi_write_enable()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000204{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000205 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = {JEDEC_WREN};
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000206
207 /* Send WREN (Write Enable) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000208 spi_command(JEDEC_WREN_OUTSIZE, JEDEC_WREN_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000209}
210
Peter Stugefa8c5502008-05-10 23:07:52 +0000211void spi_write_disable()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000212{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000213 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = {JEDEC_WRDI};
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000214
215 /* Send WRDI (Write Disable) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000216 spi_command(JEDEC_WRDI_OUTSIZE, JEDEC_WRDI_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000217}
218
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000219int probe_spi(struct flashchip *flash)
220{
221 unsigned char readarr[3];
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +0000222 uint32_t manuf_id;
223 uint32_t model_id;
Peter Stugefa8c5502008-05-10 23:07:52 +0000224 if (!spi_rdid(readarr)) {
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +0000225 /* Check if this is a continuation vendor ID */
226 if (readarr[0] == 0x7f) {
227 manuf_id = (readarr[0] << 8) | readarr[1];
228 model_id = readarr[2];
229 } else {
230 manuf_id = readarr[0];
231 model_id = (readarr[1] << 8) | readarr[2];
232 }
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000233 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id);
Carl-Daniel Hailfingere973b052008-01-04 16:22:09 +0000234 if (manuf_id == flash->manufacture_id &&
235 model_id == flash->model_id) {
236 /* Print the status register to tell the
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000237 * user about possible write protection.
238 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000239 spi_prettyprint_status_register(flash);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000240
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000241 return 1;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000242 }
Carl-Daniel Hailfingere973b052008-01-04 16:22:09 +0000243 /* Test if this is a pure vendor match. */
244 if (manuf_id == flash->manufacture_id &&
245 GENERIC_DEVICE_ID == flash->model_id)
246 return 1;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000247 }
248
249 return 0;
250}
251
Peter Stugefa8c5502008-05-10 23:07:52 +0000252uint8_t spi_read_status_register()
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000253{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000254 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = {JEDEC_RDSR};
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000255 unsigned char readarr[1];
256
257 /* Read Status Register */
Peter Stugefa8c5502008-05-10 23:07:52 +0000258 spi_command(JEDEC_RDSR_OUTSIZE, JEDEC_RDSR_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000259 return readarr[0];
260}
261
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000262/* Prettyprint the status register. Common definitions.
263 */
264void spi_prettyprint_status_register_common(uint8_t status)
265{
266 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
267 "%sset\n", (status & (1 << 5)) ? "" : "not ");
268 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
269 "%sset\n", (status & (1 << 4)) ? "" : "not ");
270 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
271 "%sset\n", (status & (1 << 3)) ? "" : "not ");
272 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
273 "%sset\n", (status & (1 << 2)) ? "" : "not ");
274 printf_debug("Chip status register: Write Enable Latch (WEL) is "
275 "%sset\n", (status & (1 << 1)) ? "" : "not ");
276 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
277 "%sset\n", (status & (1 << 0)) ? "" : "not ");
278}
279
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000280/* Prettyprint the status register. Works for
281 * ST M25P series
282 * MX MX25L series
283 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000284void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000285{
286 printf_debug("Chip status register: Status Register Write Disable "
287 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
288 printf_debug("Chip status register: Bit 6 is "
289 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000290 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000291}
292
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000293/* Prettyprint the status register. Works for
294 * SST 25VF016
295 */
296void spi_prettyprint_status_register_sst25vf016(uint8_t status)
297{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000298 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000299 "none",
300 "1F0000H-1FFFFFH",
301 "1E0000H-1FFFFFH",
302 "1C0000H-1FFFFFH",
303 "180000H-1FFFFFH",
304 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000305 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000306 };
307 printf_debug("Chip status register: Block Protect Write Disable "
308 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
309 printf_debug("Chip status register: Auto Address Increment Programming "
310 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
311 spi_prettyprint_status_register_common(status);
312 printf_debug("Resulting block protection : %s\n",
313 bpt[(status & 0x1c) >> 2]);
314}
315
316void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000317{
318 uint8_t status;
319
Peter Stugefa8c5502008-05-10 23:07:52 +0000320 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000321 printf_debug("Chip status register is %02x\n", status);
322 switch (flash->manufacture_id) {
323 case ST_ID:
324 case MX_ID:
325 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000326 spi_prettyprint_status_register_st_m25p(status);
327 break;
328 case SST_ID:
329 if (flash->model_id == SST_25VF016B)
330 spi_prettyprint_status_register_sst25vf016(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000331 break;
332 }
333}
334
Peter Stugefa8c5502008-05-10 23:07:52 +0000335int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000336{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000337 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = {JEDEC_CE_C7};
Carl-Daniel Hailfingerf5df46f2007-12-16 21:15:27 +0000338
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000339 spi_disable_blockprotect();
Peter Stugefa8c5502008-05-10 23:07:52 +0000340 spi_write_enable();
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000341 /* Send CE (Chip Erase) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000342 spi_command(JEDEC_CE_C7_OUTSIZE, JEDEC_CE_C7_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000343 /* Wait until the Write-In-Progress bit is cleared.
344 * This usually takes 1-85 s, so wait in 1 s steps.
345 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000346 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000347 sleep(1);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000348 return 0;
349}
350
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000351/* Block size is usually
352 * 64k for Macronix
353 * 32k for SST
354 * 4-32k non-uniform for EON
355 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000356int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000357{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000358 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = {JEDEC_BE_D8};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000359
360 cmd[1] = (addr & 0x00ff0000) >> 16;
361 cmd[2] = (addr & 0x0000ff00) >> 8;
362 cmd[3] = (addr & 0x000000ff);
Peter Stugefa8c5502008-05-10 23:07:52 +0000363 spi_write_enable();
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000364 /* Send BE (Block Erase) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000365 spi_command(JEDEC_BE_D8_OUTSIZE, JEDEC_BE_D8_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000366 /* Wait until the Write-In-Progress bit is cleared.
367 * This usually takes 100-4000 ms, so wait in 100 ms steps.
368 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000369 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000370 usleep(100 * 1000);
371 return 0;
372}
373
374/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Peter Stugefa8c5502008-05-10 23:07:52 +0000375int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000376{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000377 unsigned char cmd[JEDEC_SE_OUTSIZE] = {JEDEC_SE};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000378 cmd[1] = (addr & 0x00ff0000) >> 16;
379 cmd[2] = (addr & 0x0000ff00) >> 8;
380 cmd[3] = (addr & 0x000000ff);
381
Peter Stugefa8c5502008-05-10 23:07:52 +0000382 spi_write_enable();
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000383 /* Send SE (Sector Erase) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000384 spi_command(JEDEC_SE_OUTSIZE, JEDEC_SE_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000385 /* Wait until the Write-In-Progress bit is cleared.
386 * This usually takes 15-800 ms, so wait in 10 ms steps.
387 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000388 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000389 usleep(10 * 1000);
390 return 0;
391}
392
393/* Page size is usually 256 bytes */
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000394void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios) {
395 int i;
396
Peter Stugefa8c5502008-05-10 23:07:52 +0000397 spi_write_enable();
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000398 outb(0x06 , it8716f_flashport + 1);
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000399 outb(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000400 for (i = 0; i < 256; i++) {
401 bios[256 * block + i] = buf[256 * block + i];
402 }
403 outb(0, it8716f_flashport);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000404 /* Wait until the Write-In-Progress bit is cleared.
405 * This usually takes 1-10 ms, so wait in 1 ms steps.
406 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000407 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000408 usleep(1000);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000409}
410
Peter Stugefa8c5502008-05-10 23:07:52 +0000411void spi_page_program(int block, uint8_t *buf, uint8_t *bios)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000412{
413 if (it8716f_flashport)
414 it8716f_spi_page_program(block, buf, bios);
415}
416
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000417/*
418 * This is according the SST25VF016 datasheet, who knows it is more
419 * generic that this...
420 */
421void spi_write_status_register(int status)
422{
Carl-Daniel Hailfinger228231f2008-05-13 14:01:22 +0000423 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] = {JEDEC_WRSR, (unsigned char)status};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000424
425 /* Send WRSR (Write Status Register) */
Peter Stugefa8c5502008-05-10 23:07:52 +0000426 spi_command(JEDEC_WRSR_OUTSIZE, JEDEC_WRSR_INSIZE, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000427}
428
429void spi_byte_program(int address, uint8_t byte)
430{
431 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {JEDEC_BYTE_PROGRAM,
432 (address>>16)&0xff,
433 (address>>8)&0xff,
434 (address>>0)&0xff,
435 byte
436 };
437
438 /* Send Byte-Program */
Peter Stugefa8c5502008-05-10 23:07:52 +0000439 spi_command(JEDEC_BYTE_PROGRAM_OUTSIZE, JEDEC_BYTE_PROGRAM_INSIZE, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000440}
441
442void spi_disable_blockprotect(void)
443{
444 uint8_t status;
445
Peter Stugefa8c5502008-05-10 23:07:52 +0000446 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000447 /* If there is block protection in effect, unprotect it first. */
448 if ((status & 0x3c) != 0) {
449 printf_debug("Some block protection in effect, disabling\n");
Peter Stugefa8c5502008-05-10 23:07:52 +0000450 spi_write_enable();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000451 spi_write_status_register(status & ~0x3c);
452 }
453}
454
455/*
456 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
457 * Program chip using firmware cycle byte programming. (SLOW!)
458 */
459int it8716f_over512k_spi_chip_write(struct flashchip *flash, uint8_t *buf)
460{
461 int total_size = 1024 * flash->total_size;
462 int i;
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000463 fast_spi = 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000464
465 spi_disable_blockprotect();
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000466 for (i = 0; i < total_size; i++) {
Peter Stugefa8c5502008-05-10 23:07:52 +0000467 spi_write_enable();
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000468 spi_byte_program(i, buf[i]);
Peter Stugefa8c5502008-05-10 23:07:52 +0000469 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000470 myusec_delay(10);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000471 }
472 /* resume normal ops... */
473 outb(0x20, it8716f_flashport);
474 return 0;
475}
476
477void spi_3byte_read(int address, uint8_t *bytes, int len)
478{
479 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000480 (address >> 16) & 0xff,
481 (address >> 8) & 0xff,
482 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000483 };
484
485 /* Send Read */
Peter Stugefa8c5502008-05-10 23:07:52 +0000486 spi_command(JEDEC_READ_OUTSIZE, len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000487}
488
489/*
490 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
491 * Need to read this big flash using firmware cycles 3 byte at a time.
492 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000493int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000494{
495 int total_size = 1024 * flash->total_size;
496 int i;
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000497 fast_spi = 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000498
499 if (total_size > 512 * 1024) {
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000500 for (i = 0; i < total_size; i += 3) {
501 int toread = 3;
502 if (total_size - i < toread)
503 toread = total_size - i;
504 spi_3byte_read(i, buf + i, toread);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000505 }
506 } else {
507 memcpy(buf, (const char *)flash->virtual_memory, total_size);
508 }
509 return 0;
510}
511
Peter Stugefa8c5502008-05-10 23:07:52 +0000512int spi_chip_write(struct flashchip *flash, uint8_t *buf) {
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000513 int total_size = 1024 * flash->total_size;
514 int i;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000515 if (total_size > 512 * 1024) {
516 it8716f_over512k_spi_chip_write(flash, buf);
517 } else {
518 for (i = 0; i < total_size / 256; i++) {
Peter Stugefa8c5502008-05-10 23:07:52 +0000519 spi_page_program(i, buf, (uint8_t *)flash->virtual_memory);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000520 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000521 }
522 return 0;
523}
524