blob: f66d369d29d9755c4d45558341dc508b02ad5f40 [file] [log] [blame]
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9#include "flashchips.h"
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080010#include "fmap.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080011#include "gec_lpc_commands.h"
12#include "programmer.h"
13#include "spi.h"
14#include "writeprotect.h"
15
16
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080017/* 1 if we detect a GEC on system */
18static int detected = 0;
19
20/* 1 if we want the flashrom to call erase_and_write_flash() again. */
21static int need_2nd_pass = 0;
22
23/* The range of each firmware copy from the image file to update.
24 * But re-define the .flags as the valid flag to indicate the firmware is
25 * new or not (if flags = 1).
26 */
27static struct fmap_area fwcopy[4]; // [0] is not used.
28
29/* The names of enum lpc_current_image to match in FMAP area names. */
30static const char *sections[4] = {
31 "UNKNOWN SECTION", // EC_LPC_IMAGE_UNKNOWN -- never matches
32 "RO_SECTION", // EC_LPC_IMAGE_RO
33 "RW_SECTION_A", // EC_LPC_IMAGE_RW_A
34 "RW_SECTION_B", // EC_LPC_IMAGE_RW_B
35};
36
David Hendrickscb760a22012-01-05 12:33:04 -080037static int ec_timeout_usec = 1000000;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080038
39/* Waits for the EC to be unbusy. Returns 1 if busy, 0 if not busy. */
40static int ec_busy(int timeout_usec)
41{
42 int i;
43 for (i = 0; i < timeout_usec; i += 10) {
44 usleep(10); /* Delay first, in case we just sent a command */
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080045 if (!(inb(EC_LPC_ADDR_USER_CMD) & EC_LPC_STATUS_BUSY_MASK))
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080046 return 0;
47 }
48 return 1; /* Timeout */
49}
50
51
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080052static enum lpc_status gec_get_result() {
53 return inb(EC_LPC_ADDR_USER_DATA);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080054}
55
56
57/* Sends a command to the EC. Returns the command status code, or
58 * -1 if other error. */
59int ec_command(int command, const void *indata, int insize,
60 void *outdata, int outsize) {
61 uint8_t *d;
62 int i;
63
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080064 if ((insize + outsize) > EC_LPC_PARAM_SIZE) {
65 msg_pdbg2("Data size too big for buffer.\n");
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080066 return -1;
67 }
68
David Hendrickscb760a22012-01-05 12:33:04 -080069 if (ec_busy(ec_timeout_usec)) {
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080070 msg_pdbg2("Timeout waiting for EC ready\n");
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080071 return -1;
72 }
73
74 /* Write data, if any */
75 /* TODO: optimized copy using outl() */
76 for (i = 0, d = (uint8_t *)indata; i < insize; i++, d++) {
77 msg_pdbg2("GEC: Port[0x%x] <-- 0x%x\n",
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080078 EC_LPC_ADDR_USER_PARAM + i, *d);
79 outb(*d, EC_LPC_ADDR_USER_PARAM + i);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080080 }
81
82 msg_pdbg2("GEC: Run EC Command: 0x%x ----\n", command);
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080083 outb(command, EC_LPC_ADDR_USER_CMD);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080084
85 if (ec_busy(1000000)) {
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080086 msg_pdbg2("Timeout waiting for EC response\n");
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080087 return -1;
88 }
89
90 /* Check status */
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080091 if ((i = gec_get_result()) != EC_LPC_RESULT_SUCCESS) {
92 msg_pdbg2("EC returned error status %d\n", i);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080093 return i;
94 }
95
96 /* Read data, if any */
97 for (i = 0, d = (uint8_t *)outdata; i < outsize; i++, d++) {
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +080098 *d = inb(EC_LPC_ADDR_USER_PARAM + i);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080099 msg_pdbg2("GEC: Port[0x%x] ---> 0x%x\n",
Louis Yung-Chieh Loc738d9d2012-03-06 13:06:26 +0800100 EC_LPC_ADDR_USER_PARAM + i, *d);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800101 }
102
103 return 0;
104}
105
106
107#ifdef SUPPORT_CHECKSUM
108static verify_checksum(uint8_t* expected,
109 unsigned int addr,
110 unsigned int count) {
111 int rc;
112 struct lpc_params_flash_checksum csp;
113 struct lpc_response_flash_checksum csr;
114 uint8_t cs;
115 int j;
116
117 csp.offset = addr;
118 csp.size = count;
119
120 rc = ec_command(EC_LPC_COMMAND_FLASH_CHECKSUM,
121 &csp, sizeof(csp), &csr, sizeof(csr));
122 if (rc) {
123 msg_perr("GEC: verify_checksum() error.\n");
124 return rc;
125 }
126
127 for (cs = 0, j = 0; j < count; ++j) {
128 BYTE_IN(cs, expected[j]);
129 }
130 if (cs != csr.checksum) {
131 msg_pdbg("GEC: checksum dismatch at 0x%02x "
132 "(ec: 0x%02x, local: 0x%02x). Retry.\n",
133 addr, csr.checksum, cs);
134 msg_pdbg("GEC: ");
135 for (j = 0; j < count; ++j) {
136 msg_pdbg("%02x-", expected[j]);
137 if ((j & 15) == 15) msg_pdbg("\nGEC: ");
138 }
139 programmer_delay(1000);
140 return 1;
141 }
142 return 0;
143}
144#endif /* SUPPORT_CHECKSUM */
145
146
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800147/* Given the range not able to update, mark the corresponding
148 * firmware as old.
149 */
150static void gec_invalidate_copy(unsigned int addr, unsigned int len)
151{
152 int i;
153
154 for (i = EC_LPC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
155 struct fmap_area *fw = &fwcopy[i];
156 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
157 (fw->offset >= addr && (fw->offset < addr + len))) {
158 msg_pdbg("Mark firmware [%s] as old.\n",
159 sections[i]);
160 fw->flags = 0; // mark as old
161 }
162 }
163}
164
165
166/* Asks EC to jump to a firmware copy. If target is EC_LPC_IMAGE_UNKNOWN,
167 * then this functions picks a NEW firmware copy and jumps to it. Note that
168 * RO is preferred, then A, finally B.
169 *
170 * Returns 0 for success.
171 */
172static int gec_jump_copy(enum lpc_current_image target) {
173 struct lpc_params_reboot_ec p;
174 int rc;
175
Louis Yung-Chieh Lobb128ba2012-05-04 22:57:44 +0800176 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800177 p.target = target != EC_LPC_IMAGE_UNKNOWN ? target :
178 fwcopy[EC_LPC_IMAGE_RO].flags ? EC_LPC_IMAGE_RO :
179 fwcopy[EC_LPC_IMAGE_RW_A].flags ? EC_LPC_IMAGE_RW_A :
180 fwcopy[EC_LPC_IMAGE_RW_B].flags ? EC_LPC_IMAGE_RW_B :
181 EC_LPC_IMAGE_UNKNOWN;
182 msg_pdbg("GEC is jumping to [%s]\n", sections[p.target]);
183 if (p.target == EC_LPC_IMAGE_UNKNOWN) return 1;
184
185 rc = ec_command(EC_LPC_COMMAND_REBOOT_EC,
186 &p, sizeof(p), NULL, 0);
187 if (rc) {
188 msg_perr("GEC cannot jump to [%s]\n", sections[p.target]);
189 } else {
190 msg_pdbg("GEC has jumped to [%s]\n", sections[p.target]);
191 }
192
193 /* Sleep 1 sec to wait the EC re-init. */
194 usleep(1000000);
195
196 return rc;
197}
198
199
200/* Given an image, this function parses FMAP and recognize the firmware
201 * ranges.
202 */
203int gec_prepare(uint8_t *image, int size) {
204 struct fmap *fmap;
205 int i, j;
206
207 if (!detected) return 0;
208
209 // Parse the fmap in the image file and cache the firmware ranges.
210 fmap = fmap_find_in_memory(image, size);
211 if (!fmap) return 0;
212
213 // Lookup RO/A/B sections in FMAP.
214 for (i = 0; i < fmap->nareas; i++) {
215 struct fmap_area *fa = &fmap->areas[i];
216 for (j = EC_LPC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700217 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800218 msg_pdbg("Found '%s' in image.\n", fa->name);
219 memcpy(&fwcopy[j], fa, sizeof(*fa));
220 fwcopy[j].flags = 1; // mark as new
221 }
222 }
223 }
224
225 return gec_jump_copy(EC_LPC_IMAGE_RO);
226}
227
228
229/* Returns >0 if we need 2nd pass of erase_and_write_flash().
230 * <0 if we cannot jump to any firmware copy.
231 * ==0 if no more pass is needed.
232 *
233 * This function also jumps to new-updated firmware copy before return >0.
234 */
235int gec_need_2nd_pass(void) {
236 if (!detected) return 0;
237
238 if (need_2nd_pass) {
239 if (gec_jump_copy(EC_LPC_IMAGE_UNKNOWN)) {
240 return -1;
241 }
242 }
243
244 return need_2nd_pass;
245}
246
247
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800248int gec_read(struct flashchip *flash, uint8_t *readarr,
249 unsigned int blockaddr, unsigned int readcnt) {
250 int i;
251 int rc = 0;
252 struct lpc_params_flash_read p;
253 struct lpc_response_flash_read r;
254
255 for (i = 0; i < readcnt; i += EC_LPC_FLASH_SIZE_MAX) {
256 p.offset = blockaddr + i;
257 p.size = min(readcnt - i, EC_LPC_FLASH_SIZE_MAX);
258 rc = ec_command(EC_LPC_COMMAND_FLASH_READ,
259 &p, sizeof(p), &r, sizeof(r));
260 if (rc) {
261 msg_perr("GEC: Flash read error at offset 0x%x\n",
262 blockaddr + i);
263 return rc;
264 }
265
266#ifdef SUPPORT_CHECKSUM
267 if (verify_checksum(r.data, blockaddr + i,
268 min(readcnt - i, EC_LPC_FLASH_SIZE_MAX))) {
269 msg_pdbg("GEC: re-read...\n");
270 i -= EC_LPC_FLASH_SIZE_MAX;
271 continue;
272 }
273#endif
274 memcpy(readarr + i, r.data, p.size);
275 }
276
277 return rc;
278}
279
280
281static int gec_block_erase(struct flashchip *flash,
282 unsigned int blockaddr,
283 unsigned int len) {
284 struct lpc_params_flash_erase erase;
285 int rc;
David Hendricks5b06c882012-05-20 18:27:25 -0700286#ifdef SUPPORT_CHECKSUM
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800287 uint8_t *blank;
David Hendricks5b06c882012-05-20 18:27:25 -0700288#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800289
290#ifdef SUPPORT_CHECKSUM
291re_erase:
292#endif
293 erase.offset = blockaddr;
294 erase.size = len;
295 rc = ec_command(EC_LPC_COMMAND_FLASH_ERASE, &erase, sizeof(erase),
296 NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800297 if (rc == EC_LPC_RESULT_ACCESS_DENIED) {
298 // this is active image.
299 gec_invalidate_copy(blockaddr, len);
300 need_2nd_pass = 1;
301 return ACCESS_DENIED;
302 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800303 if (rc) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800304 msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n",
305 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800306 return rc;
307 }
308
309#ifdef SUPPORT_CHECKSUM
310 blank = malloc(len);
311 memset(blank, 0xff, len);
312 if (verify_checksum(blank, blockaddr, len)) {
313 msg_pdbg("GEC: Re-erase...\n");
314 goto re_erase;
315 }
316#endif
317
318 return rc;
319}
320
321
322int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
323 unsigned int nbytes) {
324 int i, rc = 0;
325 unsigned int written = 0;
326 struct lpc_params_flash_write p;
327
328 for (i = 0; i < nbytes; i += written) {
329 written = min(nbytes - i, EC_LPC_FLASH_SIZE_MAX);
330 p.offset = addr + i;
331 p.size = written;
332 memcpy(p.data, &buf[i], written);
333 rc = ec_command(EC_LPC_COMMAND_FLASH_WRITE, &p, sizeof(p),
334 NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800335 if (rc == EC_LPC_RESULT_ACCESS_DENIED) {
336 // this is active image.
337 gec_invalidate_copy(addr, nbytes);
338 need_2nd_pass = 1;
339 return ACCESS_DENIED;
340 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800341
342#ifdef SUPPORT_CHECKSUM
343 if (verify_checksum(&buf[i], addr + i, written)) {
344 msg_pdbg("GEC: re-write...\n");
345 i -= written;
346 continue;
347 }
348#endif
349
350 if (rc) break;
351 }
352
353 return rc;
354}
355
356
357static int gec_list_ranges(const struct flashchip *flash) {
358 msg_pinfo("You can specify any range:\n");
359 msg_pinfo(" from: 0x%06x, to: 0x%06x\n", 0, flash->total_size * 1024);
360 msg_pinfo(" unit: 0x%06x (%dKB)\n", 2048, 2048);
361 return 0;
362}
363
364
365static int gec_set_range(const struct flashchip *flash,
366 unsigned int start, unsigned int len) {
367 struct lpc_params_flash_wp_range p;
368 int rc;
369
370 p.offset = start;
371 p.size = len;
372 rc = ec_command(EC_LPC_COMMAND_FLASH_WP_SET_RANGE, &p, sizeof(p),
373 NULL, 0);
374 if (rc) {
375 msg_perr("GEC: wp_set_range error: rc=%d\n", rc);
376 return rc;
377 }
378
379 return 0;
380}
381
382
383static int gec_enable_writeprotect(const struct flashchip *flash) {
384 struct lpc_params_flash_wp_enable p;
385 int rc;
386
387 p.enable_wp = 1;
388 rc = ec_command(EC_LPC_COMMAND_FLASH_WP_ENABLE, &p, sizeof(p),
389 NULL, 0);
390 if (rc) {
391 msg_perr("GEC: wp_enable_wp error: rc=%d\n", rc);
392 }
393
394 return rc;
395}
396
397
398static int gec_disable_writeprotect(const struct flashchip *flash) {
399 struct lpc_params_flash_wp_enable p;
400 int rc;
401
402 p.enable_wp = 0;
403 rc = ec_command(EC_LPC_COMMAND_FLASH_WP_ENABLE, &p, sizeof(p),
404 NULL, 0);
405 if (rc) {
406 msg_perr("GEC: wp_disable_wp error: rc=%d\n", rc);
407 } else {
408 msg_pinfo("Disabled WP. Reboot EC and de-assert #WP.\n");
409 }
410
411 return rc;
412}
413
414
415static int gec_wp_status(const struct flashchip *flash) {
416 int rc;
417 struct lpc_response_flash_wp_range range;
418 struct lpc_response_flash_wp_enable en;
419 uint8_t value;
420
421 rc = ec_command(EC_LPC_COMMAND_FLASH_WP_GET_RANGE, NULL, 0,
422 &range, sizeof(range));
423 if (rc) {
424 msg_perr("GEC: wp_get_wp_range error: rc=%d\n", rc);
425 return rc;
426 }
427 rc = ec_command(EC_LPC_COMMAND_FLASH_WP_GET_STATE, NULL, 0,
428 &en, sizeof(en));
429 if (rc) {
430 msg_perr("GEC: wp_get_wp_state error: rc=%d\n", rc);
431 return rc;
432 }
433
434 /* TODO: Fix scripts which rely on SPI-specific terminology. */
435 value = (en.enable_wp << 7);
436 msg_pinfo("WP: status: 0x%02x\n", value);
437 msg_pinfo("WP: status.srp0: %x\n", en.enable_wp);
438 msg_pinfo("WP: write protect is %s.\n",
439 en.enable_wp ? "enabled" : "disabled");
440 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
441 range.offset, range.size);
442
443 return 0;
444}
445
446
447static int gec_probe_size(struct flashchip *flash) {
448 int rc;
449 struct lpc_response_flash_info info;
450 struct block_eraser *eraser;
451 static struct wp wp = {
452 .list_ranges = gec_list_ranges,
453 .set_range = gec_set_range,
454 .enable = gec_enable_writeprotect,
455 .disable = gec_disable_writeprotect,
456 .wp_status = gec_wp_status,
457 };
458
459 rc = ec_command(EC_LPC_COMMAND_FLASH_INFO, NULL, 0,
460 &info, sizeof(info));
461 if (rc) return 0;
462
463 flash->total_size = info.flash_size / 1024;
464 flash->page_size = min(info.write_block_size,
465 info.erase_block_size);
466 flash->tested = TEST_OK_PREW;
467 eraser = &flash->block_erasers[0];
468 eraser->eraseblocks[0].size = info.erase_block_size;
469 eraser->eraseblocks[0].count = info.flash_size /
470 eraser->eraseblocks[0].size;
471 flash->wp = &wp;
472
473 return 1;
474};
475
476
477static const struct opaque_programmer opaque_programmer_gec = {
478 .max_data_read = EC_LPC_FLASH_SIZE_MAX,
479 .max_data_write = EC_LPC_FLASH_SIZE_MAX,
480 .probe = gec_probe_size,
481 .read = gec_read,
482 .write = gec_write,
483 .erase = gec_block_erase,
484};
485
486
487/* Sends HELLO command to ACPI port and expects a value from Google EC.
488 *
489 * TODO: This is an intrusive command for non-Google ECs. Needs a more proper
490 * and more friendly way to detect.
491 */
492static int detect_ec(void) {
493 struct lpc_params_hello request;
494 struct lpc_response_hello response;
495 int rc = 0;
David Hendrickscb760a22012-01-05 12:33:04 -0800496 int old_timeout = ec_timeout_usec;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800497
498 if (target_bus != BUS_LPC) {
499 msg_pdbg("%s():%d target_bus is not LPC.\n", __func__, __LINE__);
500 return 1;
501 }
502
David Hendrickscb760a22012-01-05 12:33:04 -0800503 /* reduce timeout period temporarily in case EC is not present */
504 ec_timeout_usec = 25000;
505
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800506 /* Say hello to EC. */
507 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
508 rc = ec_command(EC_LPC_COMMAND_HELLO, &request, sizeof(request),
509 &response, sizeof(response));
David Hendrickscb760a22012-01-05 12:33:04 -0800510
511 ec_timeout_usec = old_timeout;
512
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800513 if (rc || response.out_data != 0xf1e2d3c4) {
514 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
515 "rc=%d, request=0x%x response=0x%x\n",
516 rc, request.in_data, response.out_data);
517#ifdef SUPPORT_CHECKSUM
518 /* In this mode, we can tolerate some bit errors. */
519 {
520 int diff = response.out_data ^ 0xf1e2d3c4;
521 if (!(diff = (diff - 1) & diff)) return 0;// 1-bit error
522 if (!(diff = (diff - 1) & diff)) return 0;// 2-bit error
523 if (!(diff = (diff - 1) & diff)) return 0;// 3-bit error
524 if (!(diff = (diff - 1) & diff)) return 0;// 4-bit error
525 }
526#endif
527 return 1;
528 }
529
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800530 detected = 1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800531 return 0;
532}
533
534/* Called by internal_init() */
535int gec_probe_programmer(const char *name) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800536 msg_pdbg("%s():%d ...\n", __func__, __LINE__);
537
538 if (detect_ec()) return 1;
539
540 register_opaque_programmer(&opaque_programmer_gec);
541 if (buses_supported & BUS_SPI) {
542 msg_pdbg("%s():%d remove BUS_SPI from buses_supported.\n",
543 __func__, __LINE__);
544 buses_supported &= ~BUS_SPI;
545 }
546 buses_supported |= BUS_LPC;
547
548 return 0;
549}