blob: dd79b93cdb86aabfd2a284fdd86eda22dee27054 [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>
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00006 * Copyright (C) 2008 coresystems GmbH
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/*
23 * Contains the ITE IT87* SPI specific routines
24 */
25
26#include <stdio.h>
27#include <pci/pci.h>
28#include <stdint.h>
29#include <string.h>
30#include "flash.h"
31#include "spi.h"
32
33#define ITE_SUPERIO_PORT1 0x2e
34#define ITE_SUPERIO_PORT2 0x4e
35
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000036uint16_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",
Uwe Hermann394131e2008-10-18 21:14:13 +000087 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis");
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000088 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000089 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis");
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000090 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000091 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis");
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000092 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000093 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis");
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000094 printf("LPC write to serial flash %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000095 (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);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000115
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000116 if (!it8716f_flashport)
117 it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000118
119 if (it8716f_flashport)
120 flashbus = BUS_TYPE_IT87XX_SPI;
121
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000122 return (!it8716f_flashport);
123}
124
Uwe Hermann394131e2008-10-18 21:14:13 +0000125/*
126 * The IT8716F only supports commands with length 1,2,4,5 bytes including
127 * command byte and can not read more than 3 bytes from the device.
128 *
129 * This function expects writearr[0] to be the first byte sent to the device,
130 * whereas the IT8716F splits commands internally into address and non-address
131 * commands with the address in inverse wire order. That's why the register
132 * ordering in case 4 and 5 may seem strange.
133 */
134int it8716f_spi_command(unsigned int writecnt, unsigned int readcnt,
135 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000136{
137 uint8_t busy, writeenc;
138 int i;
139
140 do {
Andriy Gapon65c1b862008-05-22 13:22:45 +0000141 busy = INB(it8716f_flashport) & 0x80;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000142 } while (busy);
143 if (readcnt > 3) {
144 printf("%s called with unsupported readcnt %i.\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000145 __FUNCTION__, readcnt);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000146 return 1;
147 }
148 switch (writecnt) {
149 case 1:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000150 OUTB(writearr[0], it8716f_flashport + 1);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000151 writeenc = 0x0;
152 break;
153 case 2:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000154 OUTB(writearr[0], it8716f_flashport + 1);
155 OUTB(writearr[1], it8716f_flashport + 7);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000156 writeenc = 0x1;
157 break;
158 case 4:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000159 OUTB(writearr[0], it8716f_flashport + 1);
160 OUTB(writearr[1], it8716f_flashport + 4);
161 OUTB(writearr[2], it8716f_flashport + 3);
162 OUTB(writearr[3], it8716f_flashport + 2);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000163 writeenc = 0x2;
164 break;
165 case 5:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000166 OUTB(writearr[0], it8716f_flashport + 1);
167 OUTB(writearr[1], it8716f_flashport + 4);
168 OUTB(writearr[2], it8716f_flashport + 3);
169 OUTB(writearr[3], it8716f_flashport + 2);
170 OUTB(writearr[4], it8716f_flashport + 7);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000171 writeenc = 0x3;
172 break;
173 default:
174 printf("%s called with unsupported writecnt %i.\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000175 __FUNCTION__, writecnt);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000176 return 1;
177 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000178 /*
179 * Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes.
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000180 * Note:
181 * We can't use writecnt directly, but have to use a strange encoding.
Uwe Hermann394131e2008-10-18 21:14:13 +0000182 */
183 OUTB(((0x4 + (fast_spi ? 1 : 0)) << 4)
184 | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000185
186 if (readcnt > 0) {
187 do {
Andriy Gapon65c1b862008-05-22 13:22:45 +0000188 busy = INB(it8716f_flashport) & 0x80;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000189 } while (busy);
190
Uwe Hermann394131e2008-10-18 21:14:13 +0000191 for (i = 0; i < readcnt; i++)
Andriy Gapon65c1b862008-05-22 13:22:45 +0000192 readarr[i] = INB(it8716f_flashport + 5 + i);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000193 }
194
195 return 0;
196}
197
198/* Page size is usually 256 bytes */
Uwe Hermann394131e2008-10-18 21:14:13 +0000199static void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios)
200{
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000201 int i;
202
203 spi_write_enable();
Uwe Hermann394131e2008-10-18 21:14:13 +0000204 OUTB(0x06, it8716f_flashport + 1);
Andriy Gapon65c1b862008-05-22 13:22:45 +0000205 OUTB(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000206 for (i = 0; i < 256; i++) {
207 bios[256 * block + i] = buf[256 * block + i];
208 }
Andriy Gapon65c1b862008-05-22 13:22:45 +0000209 OUTB(0, it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000210 /* Wait until the Write-In-Progress bit is cleared.
211 * This usually takes 1-10 ms, so wait in 1 ms steps.
212 */
213 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
214 usleep(1000);
215}
216
217/*
218 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
219 * Program chip using firmware cycle byte programming. (SLOW!)
220 */
221int it8716f_over512k_spi_chip_write(struct flashchip *flash, uint8_t *buf)
222{
223 int total_size = 1024 * flash->total_size;
224 int i;
Uwe Hermann394131e2008-10-18 21:14:13 +0000225
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000226 fast_spi = 0;
227
228 spi_disable_blockprotect();
229 for (i = 0; i < total_size; i++) {
230 spi_write_enable();
231 spi_byte_program(i, buf[i]);
232 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
233 myusec_delay(10);
234 }
235 /* resume normal ops... */
Andriy Gapon65c1b862008-05-22 13:22:45 +0000236 OUTB(0x20, it8716f_flashport);
Uwe Hermann394131e2008-10-18 21:14:13 +0000237
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000238 return 0;
239}
240
241/*
242 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
243 * Need to read this big flash using firmware cycles 3 byte at a time.
244 */
245int it8716f_spi_chip_read(struct flashchip *flash, uint8_t *buf)
246{
247 int total_size = 1024 * flash->total_size;
248 int i;
249 fast_spi = 0;
250
251 if (total_size > 512 * 1024) {
252 for (i = 0; i < total_size; i += 3) {
253 int toread = 3;
254 if (total_size - i < toread)
255 toread = total_size - i;
256 spi_nbyte_read(i, buf + i, toread);
257 }
258 } else {
259 memcpy(buf, (const char *)flash->virtual_memory, total_size);
260 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000261
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000262 return 0;
263}
264
Uwe Hermann394131e2008-10-18 21:14:13 +0000265int it8716f_spi_chip_write(struct flashchip *flash, uint8_t *buf)
266{
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000267 int total_size = 1024 * flash->total_size;
268 int i;
Uwe Hermann394131e2008-10-18 21:14:13 +0000269
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000270 if (total_size > 512 * 1024) {
271 it8716f_over512k_spi_chip_write(flash, buf);
272 } else {
273 for (i = 0; i < total_size / 256; i++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000274 it8716f_spi_page_program(i, buf,
275 (uint8_t *)flash->virtual_memory);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000276 }
277 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000278
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000279 return 0;
280}