blob: d7f1833c0c20b1db38d95d3e5a49806b01a9bb6b [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 */
Peter Stuged3bce832009-01-12 21:28:03 +000083 if (0x8716 == id || 0x8718 == id) {
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000084 /* 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 */
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000199static int it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios)
Uwe Hermann394131e2008-10-18 21:14:13 +0000200{
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000201 int i;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000202 int result;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000203
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000204 result = spi_write_enable();
205 if (result)
206 return result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000207 OUTB(0x06, it8716f_flashport + 1);
Andriy Gapon65c1b862008-05-22 13:22:45 +0000208 OUTB(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000209 for (i = 0; i < 256; i++) {
210 bios[256 * block + i] = buf[256 * block + i];
211 }
Andriy Gapon65c1b862008-05-22 13:22:45 +0000212 OUTB(0, it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000213 /* Wait until the Write-In-Progress bit is cleared.
214 * This usually takes 1-10 ms, so wait in 1 ms steps.
215 */
216 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
217 usleep(1000);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000218 return 0;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000219}
220
221/*
222 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
223 * Program chip using firmware cycle byte programming. (SLOW!)
224 */
225int it8716f_over512k_spi_chip_write(struct flashchip *flash, uint8_t *buf)
226{
227 int total_size = 1024 * flash->total_size;
228 int i;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000229 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000230
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000231 fast_spi = 0;
232
233 spi_disable_blockprotect();
234 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000235 result = spi_write_enable();
236 if (result)
237 return result;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000238 spi_byte_program(i, buf[i]);
239 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
240 myusec_delay(10);
241 }
242 /* resume normal ops... */
Andriy Gapon65c1b862008-05-22 13:22:45 +0000243 OUTB(0x20, it8716f_flashport);
Uwe Hermann394131e2008-10-18 21:14:13 +0000244
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000245 return 0;
246}
247
248/*
249 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
250 * Need to read this big flash using firmware cycles 3 byte at a time.
251 */
252int it8716f_spi_chip_read(struct flashchip *flash, uint8_t *buf)
253{
254 int total_size = 1024 * flash->total_size;
255 int i;
256 fast_spi = 0;
257
258 if (total_size > 512 * 1024) {
259 for (i = 0; i < total_size; i += 3) {
260 int toread = 3;
261 if (total_size - i < toread)
262 toread = total_size - i;
263 spi_nbyte_read(i, buf + i, toread);
264 }
265 } else {
266 memcpy(buf, (const char *)flash->virtual_memory, total_size);
267 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000268
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000269 return 0;
270}
271
Uwe Hermann394131e2008-10-18 21:14:13 +0000272int it8716f_spi_chip_write(struct flashchip *flash, uint8_t *buf)
273{
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000274 int total_size = 1024 * flash->total_size;
275 int i;
Uwe Hermann394131e2008-10-18 21:14:13 +0000276
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000277 if (total_size > 512 * 1024) {
278 it8716f_over512k_spi_chip_write(flash, buf);
279 } else {
280 for (i = 0; i < total_size / 256; i++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000281 it8716f_spi_page_program(i, buf,
282 (uint8_t *)flash->virtual_memory);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000283 }
284 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000285
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000286 return 0;
287}