2 * This file is part of the flashrom project.
4 * Copyright (C) 2007, 2008 Carl-Daniel Hailfinger
5 * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl>
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.
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.
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
22 * Contains the ITE IT87* SPI specific routines
32 #define ITE_SUPERIO_PORT1 0x2e
33 #define ITE_SUPERIO_PORT2 0x4e
36 uint16_t it8716f_flashport = 0;
37 /* use fast 33MHz SPI (<>0) or slow 16MHz (0) */
40 /* Generic Super I/O helper functions */
41 uint8_t regval(uint16_t port, uint8_t reg)
47 void regwrite(uint16_t port, uint8_t reg, uint8_t val)
53 /* Helper functions for most recent ITE IT87xx Super I/O chips */
54 #define CHIP_ID_BYTE1_REG 0x20
55 #define CHIP_ID_BYTE2_REG 0x21
56 static void enter_conf_mode_ite(uint16_t port)
61 if (port == ITE_SUPERIO_PORT1)
67 static void exit_conf_mode_ite(uint16_t port)
69 regwrite(port, 0x02, 0x02);
72 static uint16_t find_ite_spi_flash_port(uint16_t port)
75 uint16_t id, flashport = 0;
77 enter_conf_mode_ite(port);
79 id = regval(port, CHIP_ID_BYTE1_REG) << 8;
80 id |= regval(port, CHIP_ID_BYTE2_REG);
82 /* TODO: Handle more IT87xx if they support flash translation */
84 /* NOLDN, reg 0x24, mask out lowest bit (suspend) */
85 tmp = regval(port, 0x24) & 0xFE;
86 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
87 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis");
88 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
89 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis");
90 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
91 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis");
92 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
93 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis");
94 printf("LPC write to serial flash %sabled\n",
95 (tmp & 1 << 4) ? "en" : "dis");
96 /* If any serial flash segment is enabled, enable writing. */
97 if ((tmp & 0xe) && (!(tmp & 1 << 4))) {
98 printf("Enabling LPC write to serial flash\n");
100 regwrite(port, 0x24, tmp);
102 printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29);
103 /* LDN 0x7, reg 0x64/0x65 */
104 regwrite(port, 0x07, 0x7);
105 flashport = regval(port, 0x64) << 8;
106 flashport |= regval(port, 0x65);
108 exit_conf_mode_ite(port);
112 int it87xx_probe_spi_flash(const char *name)
114 it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1);
115 if (!it8716f_flashport)
116 it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2);
117 return (!it8716f_flashport);
120 /* The IT8716F only supports commands with length 1,2,4,5 bytes including
121 command byte and can not read more than 3 bytes from the device.
122 This function expects writearr[0] to be the first byte sent to the device,
123 whereas the IT8716F splits commands internally into address and non-address
124 commands with the address in inverse wire order. That's why the register
125 ordering in case 4 and 5 may seem strange. */
126 int it8716f_spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
128 uint8_t busy, writeenc;
132 busy = INB(it8716f_flashport) & 0x80;
135 printf("%s called with unsupported readcnt %i.\n",
136 __FUNCTION__, readcnt);
141 OUTB(writearr[0], it8716f_flashport + 1);
145 OUTB(writearr[0], it8716f_flashport + 1);
146 OUTB(writearr[1], it8716f_flashport + 7);
150 OUTB(writearr[0], it8716f_flashport + 1);
151 OUTB(writearr[1], it8716f_flashport + 4);
152 OUTB(writearr[2], it8716f_flashport + 3);
153 OUTB(writearr[3], it8716f_flashport + 2);
157 OUTB(writearr[0], it8716f_flashport + 1);
158 OUTB(writearr[1], it8716f_flashport + 4);
159 OUTB(writearr[2], it8716f_flashport + 3);
160 OUTB(writearr[3], it8716f_flashport + 2);
161 OUTB(writearr[4], it8716f_flashport + 7);
165 printf("%s called with unsupported writecnt %i.\n",
166 __FUNCTION__, writecnt);
169 /* Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes.
171 * We can't use writecnt directly, but have to use a strange encoding.
173 OUTB(((0x4 + (fast_spi ? 1 : 0)) << 4) | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport);
177 busy = INB(it8716f_flashport) & 0x80;
180 for (i = 0; i < readcnt; i++) {
181 readarr[i] = INB(it8716f_flashport + 5 + i);
188 /* Page size is usually 256 bytes */
189 void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios) {
193 OUTB(0x06 , it8716f_flashport + 1);
194 OUTB(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport);
195 for (i = 0; i < 256; i++) {
196 bios[256 * block + i] = buf[256 * block + i];
198 OUTB(0, it8716f_flashport);
199 /* Wait until the Write-In-Progress bit is cleared.
200 * This usually takes 1-10 ms, so wait in 1 ms steps.
202 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
207 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
208 * Program chip using firmware cycle byte programming. (SLOW!)
210 int it8716f_over512k_spi_chip_write(struct flashchip *flash, uint8_t *buf)
212 int total_size = 1024 * flash->total_size;
216 spi_disable_blockprotect();
217 for (i = 0; i < total_size; i++) {
219 spi_byte_program(i, buf[i]);
220 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
223 /* resume normal ops... */
224 OUTB(0x20, it8716f_flashport);
229 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
230 * Need to read this big flash using firmware cycles 3 byte at a time.
232 int it8716f_spi_chip_read(struct flashchip *flash, uint8_t *buf)
234 int total_size = 1024 * flash->total_size;
238 if (total_size > 512 * 1024) {
239 for (i = 0; i < total_size; i += 3) {
241 if (total_size - i < toread)
242 toread = total_size - i;
243 spi_nbyte_read(i, buf + i, toread);
246 memcpy(buf, (const char *)flash->virtual_memory, total_size);
251 int it8716f_spi_chip_write(struct flashchip *flash, uint8_t *buf) {
252 int total_size = 1024 * flash->total_size;
254 if (total_size > 512 * 1024) {
255 it8716f_over512k_spi_chip_write(flash, buf);
257 for (i = 0; i < total_size / 256; i++) {
258 spi_page_program(i, buf, (uint8_t *)flash->virtual_memory);