blob: d0b2e2243c1d0178f0c3d092ead98ddb6c9d8a41 [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
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000026#include <string.h>
27#include "flash.h"
28#include "spi.h"
29
30#define ITE_SUPERIO_PORT1 0x2e
31#define ITE_SUPERIO_PORT2 0x4e
32
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000033uint16_t it8716f_flashport = 0;
34/* use fast 33MHz SPI (<>0) or slow 16MHz (0) */
35int fast_spi = 1;
36
37/* Generic Super I/O helper functions */
38uint8_t regval(uint16_t port, uint8_t reg)
39{
Andriy Gapon65c1b862008-05-22 13:22:45 +000040 OUTB(reg, port);
41 return INB(port + 1);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000042}
43
44void regwrite(uint16_t port, uint8_t reg, uint8_t val)
45{
Andriy Gapon65c1b862008-05-22 13:22:45 +000046 OUTB(reg, port);
47 OUTB(val, port + 1);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000048}
49
50/* Helper functions for most recent ITE IT87xx Super I/O chips */
51#define CHIP_ID_BYTE1_REG 0x20
52#define CHIP_ID_BYTE2_REG 0x21
53static void enter_conf_mode_ite(uint16_t port)
54{
Andriy Gapon65c1b862008-05-22 13:22:45 +000055 OUTB(0x87, port);
56 OUTB(0x01, port);
57 OUTB(0x55, port);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000058 if (port == ITE_SUPERIO_PORT1)
Andriy Gapon65c1b862008-05-22 13:22:45 +000059 OUTB(0x55, port);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000060 else
Andriy Gapon65c1b862008-05-22 13:22:45 +000061 OUTB(0xaa, port);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000062}
63
64static void exit_conf_mode_ite(uint16_t port)
65{
66 regwrite(port, 0x02, 0x02);
67}
68
69static uint16_t find_ite_spi_flash_port(uint16_t port)
70{
71 uint8_t tmp = 0;
72 uint16_t id, flashport = 0;
73
74 enter_conf_mode_ite(port);
75
76 id = regval(port, CHIP_ID_BYTE1_REG) << 8;
77 id |= regval(port, CHIP_ID_BYTE2_REG);
78
79 /* TODO: Handle more IT87xx if they support flash translation */
Peter Stuged3bce832009-01-12 21:28:03 +000080 if (0x8716 == id || 0x8718 == id) {
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000081 /* NOLDN, reg 0x24, mask out lowest bit (suspend) */
82 tmp = regval(port, 0x24) & 0xFE;
83 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000084 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis");
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000085 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000086 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis");
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000087 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000088 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis");
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000089 printf("Serial flash segment 0x%08x-0x%08x %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000090 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis");
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000091 printf("LPC write to serial flash %sabled\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000092 (tmp & 1 << 4) ? "en" : "dis");
Carl-Daniel Hailfinger337df1d2008-05-16 00:19:52 +000093 /* If any serial flash segment is enabled, enable writing. */
94 if ((tmp & 0xe) && (!(tmp & 1 << 4))) {
95 printf("Enabling LPC write to serial flash\n");
96 tmp |= 1 << 4;
97 regwrite(port, 0x24, tmp);
98 }
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +000099 printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29);
100 /* 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
109int it87xx_probe_spi_flash(const char *name)
110{
111 it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000112
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000113 if (!it8716f_flashport)
114 it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000115
116 if (it8716f_flashport)
117 flashbus = BUS_TYPE_IT87XX_SPI;
118
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000119 return (!it8716f_flashport);
120}
121
Uwe Hermann394131e2008-10-18 21:14:13 +0000122/*
123 * The IT8716F only supports commands with length 1,2,4,5 bytes including
124 * command byte and can not read more than 3 bytes from the device.
125 *
126 * This function expects writearr[0] to be the first byte sent to the device,
127 * whereas the IT8716F splits commands internally into address and non-address
128 * commands with the address in inverse wire order. That's why the register
129 * ordering in case 4 and 5 may seem strange.
130 */
131int it8716f_spi_command(unsigned int writecnt, unsigned int readcnt,
132 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000133{
134 uint8_t busy, writeenc;
135 int i;
136
137 do {
Andriy Gapon65c1b862008-05-22 13:22:45 +0000138 busy = INB(it8716f_flashport) & 0x80;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000139 } while (busy);
140 if (readcnt > 3) {
141 printf("%s called with unsupported readcnt %i.\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000142 __FUNCTION__, readcnt);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000143 return 1;
144 }
145 switch (writecnt) {
146 case 1:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000147 OUTB(writearr[0], it8716f_flashport + 1);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000148 writeenc = 0x0;
149 break;
150 case 2:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000151 OUTB(writearr[0], it8716f_flashport + 1);
152 OUTB(writearr[1], it8716f_flashport + 7);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000153 writeenc = 0x1;
154 break;
155 case 4:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000156 OUTB(writearr[0], it8716f_flashport + 1);
157 OUTB(writearr[1], it8716f_flashport + 4);
158 OUTB(writearr[2], it8716f_flashport + 3);
159 OUTB(writearr[3], it8716f_flashport + 2);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000160 writeenc = 0x2;
161 break;
162 case 5:
Andriy Gapon65c1b862008-05-22 13:22:45 +0000163 OUTB(writearr[0], it8716f_flashport + 1);
164 OUTB(writearr[1], it8716f_flashport + 4);
165 OUTB(writearr[2], it8716f_flashport + 3);
166 OUTB(writearr[3], it8716f_flashport + 2);
167 OUTB(writearr[4], it8716f_flashport + 7);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000168 writeenc = 0x3;
169 break;
170 default:
171 printf("%s called with unsupported writecnt %i.\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000172 __FUNCTION__, writecnt);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000173 return 1;
174 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000175 /*
176 * Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes.
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000177 * Note:
178 * We can't use writecnt directly, but have to use a strange encoding.
Uwe Hermann394131e2008-10-18 21:14:13 +0000179 */
180 OUTB(((0x4 + (fast_spi ? 1 : 0)) << 4)
181 | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000182
183 if (readcnt > 0) {
184 do {
Andriy Gapon65c1b862008-05-22 13:22:45 +0000185 busy = INB(it8716f_flashport) & 0x80;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000186 } while (busy);
187
Uwe Hermann394131e2008-10-18 21:14:13 +0000188 for (i = 0; i < readcnt; i++)
Andriy Gapon65c1b862008-05-22 13:22:45 +0000189 readarr[i] = INB(it8716f_flashport + 5 + i);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000190 }
191
192 return 0;
193}
194
195/* Page size is usually 256 bytes */
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000196static int it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios)
Uwe Hermann394131e2008-10-18 21:14:13 +0000197{
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000198 int i;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000199 int result;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000200
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000201 result = spi_write_enable();
202 if (result)
203 return result;
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);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000215 return 0;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000216}
217
218/*
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000219 * Program chip using firmware cycle byte programming. (SLOW!)
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000220 * This is for chips which can only handle one byte writes
221 * and for chips where memory mapped programming is impossible due to
222 * size constraints in IT87* (over 512 kB)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000223 */
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000224int it8716f_spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000225{
226 int total_size = 1024 * flash->total_size;
227 int i;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000228 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000229
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000230 fast_spi = 0;
231
232 spi_disable_blockprotect();
233 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000234 result = spi_write_enable();
235 if (result)
236 return result;
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000237 spi_byte_program(i, buf[i]);
238 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
239 myusec_delay(10);
240 }
241 /* resume normal ops... */
Andriy Gapon65c1b862008-05-22 13:22:45 +0000242 OUTB(0x20, it8716f_flashport);
Uwe Hermann394131e2008-10-18 21:14:13 +0000243
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000244 return 0;
245}
246
247/*
248 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
249 * Need to read this big flash using firmware cycles 3 byte at a time.
250 */
251int it8716f_spi_chip_read(struct flashchip *flash, uint8_t *buf)
252{
253 int total_size = 1024 * flash->total_size;
254 int i;
255 fast_spi = 0;
256
257 if (total_size > 512 * 1024) {
258 for (i = 0; i < total_size; i += 3) {
259 int toread = 3;
260 if (total_size - i < toread)
261 toread = total_size - i;
262 spi_nbyte_read(i, buf + i, toread);
263 }
264 } else {
265 memcpy(buf, (const char *)flash->virtual_memory, total_size);
266 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000267
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000268 return 0;
269}
270
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000271int it8716f_spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Uwe Hermann394131e2008-10-18 21:14:13 +0000272{
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000273 int total_size = 1024 * flash->total_size;
274 int i;
Uwe Hermann394131e2008-10-18 21:14:13 +0000275
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000276 /*
277 * IT8716F only allows maximum of 512 kb SPI chip size for memory
278 * mapped access.
279 */
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000280 if (total_size > 512 * 1024) {
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000281 it8716f_spi_chip_write_1(flash, buf);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000282 } else {
283 for (i = 0; i < total_size / 256; i++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000284 it8716f_spi_page_program(i, buf,
285 (uint8_t *)flash->virtual_memory);
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000286 }
287 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000288
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000289 return 0;
290}