blob: 7e67892aeaaae64b0ccd281392ffcee85e69800e [file] [log] [blame]
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2007, 2008 Carl-Daniel Hailfinger
5 * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl>
6 *
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 ITE IT87* SPI specific routines
23 */
24
25#include <stdio.h>
26#include <pci/pci.h>
27#include <stdint.h>
28#include <string.h>
29#include "flash.h"
30#include "spi.h"
31
32#define ITE_SUPERIO_PORT1 0x2e
33#define ITE_SUPERIO_PORT2 0x4e
34
35
36uint16_t it8716f_flashport = 0;
37/* use fast 33MHz SPI (<>0) or slow 16MHz (0) */
38int fast_spi = 1;
39
40/* Generic Super I/O helper functions */
41uint8_t regval(uint16_t port, uint8_t reg)
42{
Andriy Gapon65c1b862008-05-22 13:22:45 +000043 OUTB(reg, port);
44 return INB(port + 1);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000045}
46
47void regwrite(uint16_t port, uint8_t reg, uint8_t val)
48{
Andriy Gapon65c1b862008-05-22 13:22:45 +000049 OUTB(reg, port);
50 OUTB(val, port + 1);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000051}
52
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
56static void enter_conf_mode_ite(uint16_t port)
57{
Andriy Gapon65c1b862008-05-22 13:22:45 +000058 OUTB(0x87, port);
59 OUTB(0x01, port);
60 OUTB(0x55, port);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000061 if (port == ITE_SUPERIO_PORT1)
Andriy Gapon65c1b862008-05-22 13:22:45 +000062 OUTB(0x55, port);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000063 else
Andriy Gapon65c1b862008-05-22 13:22:45 +000064 OUTB(0xaa, port);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000065}
66
67static void exit_conf_mode_ite(uint16_t port)
68{
69 regwrite(port, 0x02, 0x02);
70}
71
72static uint16_t find_ite_spi_flash_port(uint16_t port)
73{
74 uint8_t tmp = 0;
75 uint16_t id, flashport = 0;
76
77 enter_conf_mode_ite(port);
78
79 id = regval(port, CHIP_ID_BYTE1_REG) << 8;
80 id |= regval(port, CHIP_ID_BYTE2_REG);
81
82 /* TODO: Handle more IT87xx if they support flash translation */
83 if (id == 0x8716) {
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");
Carl-Daniel Hailfinger337df1d2008-05-16 00:19:52 +000096 /* 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");
99 tmp |= 1 << 4;
100 regwrite(port, 0x24, tmp);
101 }
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000102 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);
107 }
108 exit_conf_mode_ite(port);
109 return flashport;
110}
111
112int it87xx_probe_spi_flash(const char *name)
113{
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);
118}
119
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. */
126int it8716f_spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
127{
128 uint8_t busy, writeenc;
129 int i;
130
131 do {
Andriy Gapon65c1b862008-05-22 13:22:45 +0000132 busy = INB(it8716f_flashport) & 0x80;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000133 } while (busy);
134 if (readcnt > 3) {
135 printf("%s called with unsupported readcnt %i.\n",
136 __FUNCTION__, readcnt);
137 return 1;
138 }
139 switch (writecnt) {
140 case 1:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000141 OUTB(writearr[0], it8716f_flashport + 1);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000142 writeenc = 0x0;
143 break;
144 case 2:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000145 OUTB(writearr[0], it8716f_flashport + 1);
146 OUTB(writearr[1], it8716f_flashport + 7);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000147 writeenc = 0x1;
148 break;
149 case 4:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000150 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);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000154 writeenc = 0x2;
155 break;
156 case 5:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000157 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);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000162 writeenc = 0x3;
163 break;
164 default:
165 printf("%s called with unsupported writecnt %i.\n",
166 __FUNCTION__, writecnt);
167 return 1;
168 }
169 /* Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes.
170 * Note:
171 * We can't use writecnt directly, but have to use a strange encoding.
172 */
Andriy Gapon65c1b862008-05-22 13:22:45 +0000173 OUTB(((0x4 + (fast_spi ? 1 : 0)) << 4) | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000174
175 if (readcnt > 0) {
176 do {
Andriy Gapon65c1b862008-05-22 13:22:45 +0000177 busy = INB(it8716f_flashport) & 0x80;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000178 } while (busy);
179
180 for (i = 0; i < readcnt; i++) {
Andriy Gapon65c1b862008-05-22 13:22:45 +0000181 readarr[i] = INB(it8716f_flashport + 5 + i);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000182 }
183 }
184
185 return 0;
186}
187
188/* Page size is usually 256 bytes */
189void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios) {
190 int i;
191
192 spi_write_enable();
Andriy Gapon65c1b862008-05-22 13:22:45 +0000193 OUTB(0x06 , it8716f_flashport + 1);
194 OUTB(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000195 for (i = 0; i < 256; i++) {
196 bios[256 * block + i] = buf[256 * block + i];
197 }
Andriy Gapon65c1b862008-05-22 13:22:45 +0000198 OUTB(0, it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000199 /* Wait until the Write-In-Progress bit is cleared.
200 * This usually takes 1-10 ms, so wait in 1 ms steps.
201 */
202 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
203 usleep(1000);
204}
205
206/*
207 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
208 * Program chip using firmware cycle byte programming. (SLOW!)
209 */
210int it8716f_over512k_spi_chip_write(struct flashchip *flash, uint8_t *buf)
211{
212 int total_size = 1024 * flash->total_size;
213 int i;
214 fast_spi = 0;
215
216 spi_disable_blockprotect();
217 for (i = 0; i < total_size; i++) {
218 spi_write_enable();
219 spi_byte_program(i, buf[i]);
220 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
221 myusec_delay(10);
222 }
223 /* resume normal ops... */
Andriy Gapon65c1b862008-05-22 13:22:45 +0000224 OUTB(0x20, it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000225 return 0;
226}
227
228/*
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.
231 */
232int it8716f_spi_chip_read(struct flashchip *flash, uint8_t *buf)
233{
234 int total_size = 1024 * flash->total_size;
235 int i;
236 fast_spi = 0;
237
238 if (total_size > 512 * 1024) {
239 for (i = 0; i < total_size; i += 3) {
240 int toread = 3;
241 if (total_size - i < toread)
242 toread = total_size - i;
243 spi_nbyte_read(i, buf + i, toread);
244 }
245 } else {
246 memcpy(buf, (const char *)flash->virtual_memory, total_size);
247 }
248 return 0;
249}
250
251int it8716f_spi_chip_write(struct flashchip *flash, uint8_t *buf) {
252 int total_size = 1024 * flash->total_size;
253 int i;
254 if (total_size > 512 * 1024) {
255 it8716f_over512k_spi_chip_write(flash, buf);
256 } else {
257 for (i = 0; i < total_size / 256; i++) {
258 spi_page_program(i, buf, (uint8_t *)flash->virtual_memory);
259 }
260 }
261 return 0;
262}
263