xref: /rk3399_rockchip-uboot/drivers/misc/cros_ec.c (revision 4ff9b461a8c50245a611b15a4fa67784cc452a3d)
1 /*
2  * Chromium OS cros_ec driver
3  *
4  * Copyright (c) 2012 The Chromium OS Authors.
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 /*
10  * The Matrix Keyboard Protocol driver handles talking to the keyboard
11  * controller chip. Mostly this is for keyboard functions, but some other
12  * things have slipped in, so we provide generic services to talk to the
13  * KBC.
14  */
15 
16 #include <common.h>
17 #include <command.h>
18 #include <i2c.h>
19 #include <cros_ec.h>
20 #include <fdtdec.h>
21 #include <malloc.h>
22 #include <spi.h>
23 #include <asm/io.h>
24 #include <asm-generic/gpio.h>
25 
26 #ifdef DEBUG_TRACE
27 #define debug_trace(fmt, b...)	debug(fmt, #b)
28 #else
29 #define debug_trace(fmt, b...)
30 #endif
31 
32 enum {
33 	/* Timeout waiting for a flash erase command to complete */
34 	CROS_EC_CMD_TIMEOUT_MS	= 5000,
35 	/* Timeout waiting for a synchronous hash to be recomputed */
36 	CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
37 };
38 
39 static struct cros_ec_dev static_dev, *last_dev;
40 
41 DECLARE_GLOBAL_DATA_PTR;
42 
43 /* Note: depends on enum ec_current_image */
44 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
45 
46 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
47 {
48 #ifdef DEBUG
49 	int i;
50 
51 	printf("%s: ", name);
52 	if (cmd != -1)
53 		printf("cmd=%#x: ", cmd);
54 	for (i = 0; i < len; i++)
55 		printf("%02x ", data[i]);
56 	printf("\n");
57 #endif
58 }
59 
60 /*
61  * Calculate a simple 8-bit checksum of a data block
62  *
63  * @param data	Data block to checksum
64  * @param size	Size of data block in bytes
65  * @return checksum value (0 to 255)
66  */
67 int cros_ec_calc_checksum(const uint8_t *data, int size)
68 {
69 	int csum, i;
70 
71 	for (i = csum = 0; i < size; i++)
72 		csum += data[i];
73 	return csum & 0xff;
74 }
75 
76 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
77 			const void *dout, int dout_len,
78 			uint8_t **dinp, int din_len)
79 {
80 	int ret;
81 
82 	switch (dev->interface) {
83 #ifdef CONFIG_CROS_EC_SPI
84 	case CROS_EC_IF_SPI:
85 		ret = cros_ec_spi_command(dev, cmd, cmd_version,
86 					(const uint8_t *)dout, dout_len,
87 					dinp, din_len);
88 		break;
89 #endif
90 #ifdef CONFIG_CROS_EC_I2C
91 	case CROS_EC_IF_I2C:
92 		ret = cros_ec_i2c_command(dev, cmd, cmd_version,
93 					(const uint8_t *)dout, dout_len,
94 					dinp, din_len);
95 		break;
96 #endif
97 #ifdef CONFIG_CROS_EC_LPC
98 	case CROS_EC_IF_LPC:
99 		ret = cros_ec_lpc_command(dev, cmd, cmd_version,
100 					(const uint8_t *)dout, dout_len,
101 					dinp, din_len);
102 		break;
103 #endif
104 	case CROS_EC_IF_NONE:
105 	default:
106 		ret = -1;
107 	}
108 
109 	return ret;
110 }
111 
112 /**
113  * Send a command to the CROS-EC device and return the reply.
114  *
115  * The device's internal input/output buffers are used.
116  *
117  * @param dev		CROS-EC device
118  * @param cmd		Command to send (EC_CMD_...)
119  * @param cmd_version	Version of command to send (EC_VER_...)
120  * @param dout          Output data (may be NULL If dout_len=0)
121  * @param dout_len      Size of output data in bytes
122  * @param dinp          Response data (may be NULL If din_len=0).
123  *			If not NULL, it will be updated to point to the data
124  *			and will always be double word aligned (64-bits)
125  * @param din_len       Maximum size of response in bytes
126  * @return number of bytes in response, or -1 on error
127  */
128 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
129 		int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
130 		int din_len)
131 {
132 	uint8_t *din;
133 	int len;
134 
135 	len = send_command(dev, cmd, cmd_version, dout, dout_len,
136 				&din, din_len);
137 
138 	/* If the command doesn't complete, wait a while */
139 	if (len == -EC_RES_IN_PROGRESS) {
140 		struct ec_response_get_comms_status *resp;
141 		ulong start;
142 
143 		/* Wait for command to complete */
144 		start = get_timer(0);
145 		do {
146 			int ret;
147 
148 			mdelay(50);	/* Insert some reasonable delay */
149 			ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
150 					NULL, 0,
151 					(uint8_t **)&resp, sizeof(*resp));
152 			if (ret < 0)
153 				return ret;
154 
155 			if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
156 				debug("%s: Command %#02x timeout\n",
157 				      __func__, cmd);
158 				return -EC_RES_TIMEOUT;
159 			}
160 		} while (resp->flags & EC_COMMS_STATUS_PROCESSING);
161 
162 		/* OK it completed, so read the status response */
163 		/* not sure why it was 0 for the last argument */
164 		len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
165 				NULL, 0, &din, din_len);
166 	}
167 
168 	debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, *dinp);
169 	if (dinp) {
170 		/* If we have any data to return, it must be 64bit-aligned */
171 		assert(len <= 0 || !((uintptr_t)din & 7));
172 		*dinp = din;
173 	}
174 
175 	return len;
176 }
177 
178 /**
179  * Send a command to the CROS-EC device and return the reply.
180  *
181  * The device's internal input/output buffers are used.
182  *
183  * @param dev		CROS-EC device
184  * @param cmd		Command to send (EC_CMD_...)
185  * @param cmd_version	Version of command to send (EC_VER_...)
186  * @param dout          Output data (may be NULL If dout_len=0)
187  * @param dout_len      Size of output data in bytes
188  * @param din           Response data (may be NULL If din_len=0).
189  *			It not NULL, it is a place for ec_command() to copy the
190  *      data to.
191  * @param din_len       Maximum size of response in bytes
192  * @return number of bytes in response, or -1 on error
193  */
194 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
195 		      const void *dout, int dout_len,
196 		      void *din, int din_len)
197 {
198 	uint8_t *in_buffer;
199 	int len;
200 
201 	assert((din_len == 0) || din);
202 	len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
203 			&in_buffer, din_len);
204 	if (len > 0) {
205 		/*
206 		 * If we were asked to put it somewhere, do so, otherwise just
207 		 * disregard the result.
208 		 */
209 		if (din && in_buffer) {
210 			assert(len <= din_len);
211 			memmove(din, in_buffer, len);
212 		}
213 	}
214 	return len;
215 }
216 
217 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
218 {
219 	if (ec_command(dev, EC_CMD_CROS_EC_STATE, 0, NULL, 0, scan,
220 		       sizeof(scan->data)) < sizeof(scan->data))
221 		return -1;
222 
223 	return 0;
224 }
225 
226 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
227 {
228 	struct ec_response_get_version *r;
229 
230 	if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
231 			(uint8_t **)&r, sizeof(*r)) < sizeof(*r))
232 		return -1;
233 
234 	if (maxlen > sizeof(r->version_string_ro))
235 		maxlen = sizeof(r->version_string_ro);
236 
237 	switch (r->current_image) {
238 	case EC_IMAGE_RO:
239 		memcpy(id, r->version_string_ro, maxlen);
240 		break;
241 	case EC_IMAGE_RW:
242 		memcpy(id, r->version_string_rw, maxlen);
243 		break;
244 	default:
245 		return -1;
246 	}
247 
248 	id[maxlen - 1] = '\0';
249 	return 0;
250 }
251 
252 int cros_ec_read_version(struct cros_ec_dev *dev,
253 		       struct ec_response_get_version **versionp)
254 {
255 	if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
256 			(uint8_t **)versionp, sizeof(**versionp))
257 			< sizeof(**versionp))
258 		return -1;
259 
260 	return 0;
261 }
262 
263 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
264 {
265 	if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
266 			(uint8_t **)strp, EC_HOST_PARAM_SIZE) < 0)
267 		return -1;
268 
269 	return 0;
270 }
271 
272 int cros_ec_read_current_image(struct cros_ec_dev *dev,
273 		enum ec_current_image *image)
274 {
275 	struct ec_response_get_version *r;
276 
277 	if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
278 			(uint8_t **)&r, sizeof(*r)) < sizeof(*r))
279 		return -1;
280 
281 	*image = r->current_image;
282 	return 0;
283 }
284 
285 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
286 				  struct ec_response_vboot_hash *hash)
287 {
288 	struct ec_params_vboot_hash p;
289 	ulong start;
290 
291 	start = get_timer(0);
292 	while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
293 		mdelay(50);	/* Insert some reasonable delay */
294 
295 		p.cmd = EC_VBOOT_HASH_GET;
296 		if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
297 		       hash, sizeof(*hash)) < 0)
298 			return -1;
299 
300 		if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
301 			debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
302 			return -EC_RES_TIMEOUT;
303 		}
304 	}
305 	return 0;
306 }
307 
308 
309 int cros_ec_read_hash(struct cros_ec_dev *dev,
310 		struct ec_response_vboot_hash *hash)
311 {
312 	struct ec_params_vboot_hash p;
313 	int rv;
314 
315 	p.cmd = EC_VBOOT_HASH_GET;
316 	if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
317 		       hash, sizeof(*hash)) < 0)
318 		return -1;
319 
320 	/* If the EC is busy calculating the hash, fidget until it's done. */
321 	rv = cros_ec_wait_on_hash_done(dev, hash);
322 	if (rv)
323 		return rv;
324 
325 	/* If the hash is valid, we're done. Otherwise, we have to kick it off
326 	 * again and wait for it to complete. Note that we explicitly assume
327 	 * that hashing zero bytes is always wrong, even though that would
328 	 * produce a valid hash value. */
329 	if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
330 		return 0;
331 
332 	debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
333 	      __func__, hash->status, hash->size);
334 
335 	p.cmd = EC_VBOOT_HASH_RECALC;
336 	p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
337 	p.nonce_size = 0;
338 	p.offset = EC_VBOOT_HASH_OFFSET_RW;
339 
340 	if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
341 		       hash, sizeof(*hash)) < 0)
342 		return -1;
343 
344 	rv = cros_ec_wait_on_hash_done(dev, hash);
345 	if (rv)
346 		return rv;
347 
348 	debug("%s: hash done\n", __func__);
349 
350 	return 0;
351 }
352 
353 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
354 {
355 	struct ec_params_vboot_hash p;
356 	struct ec_response_vboot_hash *hash;
357 
358 	/* We don't have an explict command for the EC to discard its current
359 	 * hash value, so we'll just tell it to calculate one that we know is
360 	 * wrong (we claim that hashing zero bytes is always invalid).
361 	 */
362 	p.cmd = EC_VBOOT_HASH_RECALC;
363 	p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
364 	p.nonce_size = 0;
365 	p.offset = 0;
366 	p.size = 0;
367 
368 	debug("%s:\n", __func__);
369 
370 	if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
371 		       (uint8_t **)&hash, sizeof(*hash)) < 0)
372 		return -1;
373 
374 	/* No need to wait for it to finish */
375 	return 0;
376 }
377 
378 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
379 		uint8_t flags)
380 {
381 	struct ec_params_reboot_ec p;
382 
383 	p.cmd = cmd;
384 	p.flags = flags;
385 
386 	if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
387 			< 0)
388 		return -1;
389 
390 	if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
391 		/*
392 		 * EC reboot will take place immediately so delay to allow it
393 		 * to complete.  Note that some reboot types (EC_REBOOT_COLD)
394 		 * will reboot the AP as well, in which case we won't actually
395 		 * get to this point.
396 		 */
397 		/*
398 		 * TODO(rspangler@chromium.org): Would be nice if we had a
399 		 * better way to determine when the reboot is complete.  Could
400 		 * we poll a memory-mapped LPC value?
401 		 */
402 		udelay(50000);
403 	}
404 
405 	return 0;
406 }
407 
408 int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
409 {
410 	/* no interrupt support : always poll */
411 	if (!fdt_gpio_isvalid(&dev->ec_int))
412 		return 1;
413 
414 	return !gpio_get_value(dev->ec_int.gpio);
415 }
416 
417 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_cros_ec_info *info)
418 {
419 	if (ec_command(dev, EC_CMD_CROS_EC_INFO, 0, NULL, 0, info,
420 			sizeof(*info)) < sizeof(*info))
421 		return -1;
422 
423 	return 0;
424 }
425 
426 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
427 {
428 	struct ec_response_host_event_mask *resp;
429 
430 	/*
431 	 * Use the B copy of the event flags, because the main copy is already
432 	 * used by ACPI/SMI.
433 	 */
434 	if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
435 		       (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
436 		return -1;
437 
438 	if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
439 		return -1;
440 
441 	*events_ptr = resp->mask;
442 	return 0;
443 }
444 
445 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
446 {
447 	struct ec_params_host_event_mask params;
448 
449 	params.mask = events;
450 
451 	/*
452 	 * Use the B copy of the event flags, so it affects the data returned
453 	 * by cros_ec_get_host_events().
454 	 */
455 	if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
456 		       &params, sizeof(params), NULL, 0) < 0)
457 		return -1;
458 
459 	return 0;
460 }
461 
462 int cros_ec_flash_protect(struct cros_ec_dev *dev,
463 		       uint32_t set_mask, uint32_t set_flags,
464 		       struct ec_response_flash_protect *resp)
465 {
466 	struct ec_params_flash_protect params;
467 
468 	params.mask = set_mask;
469 	params.flags = set_flags;
470 
471 	if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
472 		       &params, sizeof(params),
473 		       resp, sizeof(*resp)) < sizeof(*resp))
474 		return -1;
475 
476 	return 0;
477 }
478 
479 static int cros_ec_check_version(struct cros_ec_dev *dev)
480 {
481 	struct ec_params_hello req;
482 	struct ec_response_hello *resp;
483 
484 #ifdef CONFIG_CROS_EC_LPC
485 	/* LPC has its own way of doing this */
486 	if (dev->interface == CROS_EC_IF_LPC)
487 		return cros_ec_lpc_check_version(dev);
488 #endif
489 
490 	/*
491 	 * TODO(sjg@chromium.org).
492 	 * There is a strange oddity here with the EC. We could just ignore
493 	 * the response, i.e. pass the last two parameters as NULL and 0.
494 	 * In this case we won't read back very many bytes from the EC.
495 	 * On the I2C bus the EC gets upset about this and will try to send
496 	 * the bytes anyway. This means that we will have to wait for that
497 	 * to complete before continuing with a new EC command.
498 	 *
499 	 * This problem is probably unique to the I2C bus.
500 	 *
501 	 * So for now, just read all the data anyway.
502 	 */
503 	dev->cmd_version_is_supported = 1;
504 	if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
505 		       (uint8_t **)&resp, sizeof(*resp)) > 0) {
506 		/* It appears to understand new version commands */
507 		dev->cmd_version_is_supported = 1;
508 	} else {
509 		printf("%s: ERROR: old EC interface not supported\n",
510 		       __func__);
511 		return -1;
512 	}
513 
514 	return 0;
515 }
516 
517 int cros_ec_test(struct cros_ec_dev *dev)
518 {
519 	struct ec_params_hello req;
520 	struct ec_response_hello *resp;
521 
522 	req.in_data = 0x12345678;
523 	if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
524 		       (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
525 		printf("ec_command_inptr() returned error\n");
526 		return -1;
527 	}
528 	if (resp->out_data != req.in_data + 0x01020304) {
529 		printf("Received invalid handshake %x\n", resp->out_data);
530 		return -1;
531 	}
532 
533 	return 0;
534 }
535 
536 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
537 		      uint32_t *offset, uint32_t *size)
538 {
539 	struct ec_params_flash_region_info p;
540 	struct ec_response_flash_region_info *r;
541 	int ret;
542 
543 	p.region = region;
544 	ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
545 			 EC_VER_FLASH_REGION_INFO,
546 			 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
547 	if (ret != sizeof(*r))
548 		return -1;
549 
550 	if (offset)
551 		*offset = r->offset;
552 	if (size)
553 		*size = r->size;
554 
555 	return 0;
556 }
557 
558 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
559 {
560 	struct ec_params_flash_erase p;
561 
562 	p.offset = offset;
563 	p.size = size;
564 	return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
565 			NULL, 0);
566 }
567 
568 /**
569  * Write a single block to the flash
570  *
571  * Write a block of data to the EC flash. The size must not exceed the flash
572  * write block size which you can obtain from cros_ec_flash_write_burst_size().
573  *
574  * The offset starts at 0. You can obtain the region information from
575  * cros_ec_flash_offset() to find out where to write for a particular region.
576  *
577  * Attempting to write to the region where the EC is currently running from
578  * will result in an error.
579  *
580  * @param dev		CROS-EC device
581  * @param data		Pointer to data buffer to write
582  * @param offset	Offset within flash to write to.
583  * @param size		Number of bytes to write
584  * @return 0 if ok, -1 on error
585  */
586 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
587 		const uint8_t *data, uint32_t offset, uint32_t size)
588 {
589 	struct ec_params_flash_write p;
590 
591 	p.offset = offset;
592 	p.size = size;
593 	assert(data && p.size <= sizeof(p.data));
594 	memcpy(p.data, data, p.size);
595 
596 	return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
597 			  &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
598 }
599 
600 /**
601  * Return optimal flash write burst size
602  */
603 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
604 {
605 	struct ec_params_flash_write p;
606 	return sizeof(p.data);
607 }
608 
609 /**
610  * Check if a block of data is erased (all 0xff)
611  *
612  * This function is useful when dealing with flash, for checking whether a
613  * data block is erased and thus does not need to be programmed.
614  *
615  * @param data		Pointer to data to check (must be word-aligned)
616  * @param size		Number of bytes to check (must be word-aligned)
617  * @return 0 if erased, non-zero if any word is not erased
618  */
619 static int cros_ec_data_is_erased(const uint32_t *data, int size)
620 {
621 	assert(!(size & 3));
622 	size /= sizeof(uint32_t);
623 	for (; size > 0; size -= 4, data++)
624 		if (*data != -1U)
625 			return 0;
626 
627 	return 1;
628 }
629 
630 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
631 		     uint32_t offset, uint32_t size)
632 {
633 	uint32_t burst = cros_ec_flash_write_burst_size(dev);
634 	uint32_t end, off;
635 	int ret;
636 
637 	/*
638 	 * TODO: round up to the nearest multiple of write size.  Can get away
639 	 * without that on link right now because its write size is 4 bytes.
640 	 */
641 	end = offset + size;
642 	for (off = offset; off < end; off += burst, data += burst) {
643 		uint32_t todo;
644 
645 		/* If the data is empty, there is no point in programming it */
646 		todo = min(end - off, burst);
647 		if (dev->optimise_flash_write &&
648 				cros_ec_data_is_erased((uint32_t *)data, todo))
649 			continue;
650 
651 		ret = cros_ec_flash_write_block(dev, data, off, todo);
652 		if (ret)
653 			return ret;
654 	}
655 
656 	return 0;
657 }
658 
659 /**
660  * Read a single block from the flash
661  *
662  * Read a block of data from the EC flash. The size must not exceed the flash
663  * write block size which you can obtain from cros_ec_flash_write_burst_size().
664  *
665  * The offset starts at 0. You can obtain the region information from
666  * cros_ec_flash_offset() to find out where to read for a particular region.
667  *
668  * @param dev		CROS-EC device
669  * @param data		Pointer to data buffer to read into
670  * @param offset	Offset within flash to read from
671  * @param size		Number of bytes to read
672  * @return 0 if ok, -1 on error
673  */
674 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
675 				 uint32_t offset, uint32_t size)
676 {
677 	struct ec_params_flash_read p;
678 
679 	p.offset = offset;
680 	p.size = size;
681 
682 	return ec_command(dev, EC_CMD_FLASH_READ, 0,
683 			  &p, sizeof(p), data, size) >= 0 ? 0 : -1;
684 }
685 
686 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
687 		    uint32_t size)
688 {
689 	uint32_t burst = cros_ec_flash_write_burst_size(dev);
690 	uint32_t end, off;
691 	int ret;
692 
693 	end = offset + size;
694 	for (off = offset; off < end; off += burst, data += burst) {
695 		ret = cros_ec_flash_read_block(dev, data, off,
696 					    min(end - off, burst));
697 		if (ret)
698 			return ret;
699 	}
700 
701 	return 0;
702 }
703 
704 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
705 			 const uint8_t *image, int image_size)
706 {
707 	uint32_t rw_offset, rw_size;
708 	int ret;
709 
710 	if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
711 		return -1;
712 	if (image_size > rw_size)
713 		return -1;
714 
715 	/* Invalidate the existing hash, just in case the AP reboots
716 	 * unexpectedly during the update. If that happened, the EC RW firmware
717 	 * would be invalid, but the EC would still have the original hash.
718 	 */
719 	ret = cros_ec_invalidate_hash(dev);
720 	if (ret)
721 		return ret;
722 
723 	/*
724 	 * Erase the entire RW section, so that the EC doesn't see any garbage
725 	 * past the new image if it's smaller than the current image.
726 	 *
727 	 * TODO: could optimize this to erase just the current image, since
728 	 * presumably everything past that is 0xff's.  But would still need to
729 	 * round up to the nearest multiple of erase size.
730 	 */
731 	ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
732 	if (ret)
733 		return ret;
734 
735 	/* Write the image */
736 	ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
737 	if (ret)
738 		return ret;
739 
740 	return 0;
741 }
742 
743 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
744 {
745 	struct ec_params_vbnvcontext p;
746 	int len;
747 
748 	p.op = EC_VBNV_CONTEXT_OP_READ;
749 
750 	len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
751 			&p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
752 	if (len < EC_VBNV_BLOCK_SIZE)
753 		return -1;
754 
755 	return 0;
756 }
757 
758 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
759 {
760 	struct ec_params_vbnvcontext p;
761 	int len;
762 
763 	p.op = EC_VBNV_CONTEXT_OP_WRITE;
764 	memcpy(p.block, block, sizeof(p.block));
765 
766 	len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
767 			&p, sizeof(p), NULL, 0);
768 	if (len < 0)
769 		return -1;
770 
771 	return 0;
772 }
773 
774 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
775 {
776 	struct ec_params_ldo_set params;
777 
778 	params.index = index;
779 	params.state = state;
780 
781 	if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
782 		       &params, sizeof(params),
783 		       NULL, 0))
784 		return -1;
785 
786 	return 0;
787 }
788 
789 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
790 {
791 	struct ec_params_ldo_get params;
792 	struct ec_response_ldo_get *resp;
793 
794 	params.index = index;
795 
796 	if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
797 		       &params, sizeof(params),
798 		       (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
799 		return -1;
800 
801 	*state = resp->state;
802 
803 	return 0;
804 }
805 
806 /**
807  * Decode MBKP details from the device tree and allocate a suitable device.
808  *
809  * @param blob		Device tree blob
810  * @param node		Node to decode from
811  * @param devp		Returns a pointer to the new allocated device
812  * @return 0 if ok, -1 on error
813  */
814 static int cros_ec_decode_fdt(const void *blob, int node,
815 		struct cros_ec_dev **devp)
816 {
817 	enum fdt_compat_id compat;
818 	struct cros_ec_dev *dev;
819 	int parent;
820 
821 	/* See what type of parent we are inside (this is expensive) */
822 	parent = fdt_parent_offset(blob, node);
823 	if (parent < 0) {
824 		debug("%s: Cannot find node parent\n", __func__);
825 		return -1;
826 	}
827 
828 	dev = &static_dev;
829 	dev->node = node;
830 	dev->parent_node = parent;
831 
832 	compat = fdtdec_lookup(blob, parent);
833 	switch (compat) {
834 #ifdef CONFIG_CROS_EC_SPI
835 	case COMPAT_SAMSUNG_EXYNOS_SPI:
836 		dev->interface = CROS_EC_IF_SPI;
837 		if (cros_ec_spi_decode_fdt(dev, blob))
838 			return -1;
839 		break;
840 #endif
841 #ifdef CONFIG_CROS_EC_I2C
842 	case COMPAT_SAMSUNG_S3C2440_I2C:
843 		dev->interface = CROS_EC_IF_I2C;
844 		if (cros_ec_i2c_decode_fdt(dev, blob))
845 			return -1;
846 		break;
847 #endif
848 #ifdef CONFIG_CROS_EC_LPC
849 	case COMPAT_INTEL_LPC:
850 		dev->interface = CROS_EC_IF_LPC;
851 		break;
852 #endif
853 	default:
854 		debug("%s: Unknown compat id %d\n", __func__, compat);
855 		return -1;
856 	}
857 
858 	fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
859 	dev->optimise_flash_write = fdtdec_get_bool(blob, node,
860 						    "optimise-flash-write");
861 	*devp = dev;
862 
863 	return 0;
864 }
865 
866 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
867 {
868 	char id[MSG_BYTES];
869 	struct cros_ec_dev *dev;
870 	int node = 0;
871 
872 	*cros_ecp = NULL;
873 	do {
874 		node = fdtdec_next_compatible(blob, node,
875 					      COMPAT_GOOGLE_CROS_EC);
876 		if (node < 0) {
877 			debug("%s: Node not found\n", __func__);
878 			return 0;
879 		}
880 	} while (!fdtdec_get_is_enabled(blob, node));
881 
882 	if (cros_ec_decode_fdt(blob, node, &dev)) {
883 		debug("%s: Failed to decode device.\n", __func__);
884 		return -CROS_EC_ERR_FDT_DECODE;
885 	}
886 
887 	switch (dev->interface) {
888 #ifdef CONFIG_CROS_EC_SPI
889 	case CROS_EC_IF_SPI:
890 		if (cros_ec_spi_init(dev, blob)) {
891 			debug("%s: Could not setup SPI interface\n", __func__);
892 			return -CROS_EC_ERR_DEV_INIT;
893 		}
894 		break;
895 #endif
896 #ifdef CONFIG_CROS_EC_I2C
897 	case CROS_EC_IF_I2C:
898 		if (cros_ec_i2c_init(dev, blob))
899 			return -CROS_EC_ERR_DEV_INIT;
900 		break;
901 #endif
902 #ifdef CONFIG_CROS_EC_LPC
903 	case CROS_EC_IF_LPC:
904 		if (cros_ec_lpc_init(dev, blob))
905 			return -CROS_EC_ERR_DEV_INIT;
906 		break;
907 #endif
908 	case CROS_EC_IF_NONE:
909 	default:
910 		return 0;
911 	}
912 
913 	/* we will poll the EC interrupt line */
914 	fdtdec_setup_gpio(&dev->ec_int);
915 	if (fdt_gpio_isvalid(&dev->ec_int))
916 		gpio_direction_input(dev->ec_int.gpio);
917 
918 	if (cros_ec_check_version(dev)) {
919 		debug("%s: Could not detect CROS-EC version\n", __func__);
920 		return -CROS_EC_ERR_CHECK_VERSION;
921 	}
922 
923 	if (cros_ec_read_id(dev, id, sizeof(id))) {
924 		debug("%s: Could not read KBC ID\n", __func__);
925 		return -CROS_EC_ERR_READ_ID;
926 	}
927 
928 	/* Remember this device for use by the cros_ec command */
929 	last_dev = *cros_ecp = dev;
930 	debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
931 
932 	return 0;
933 }
934 
935 #ifdef CONFIG_CMD_CROS_EC
936 int cros_ec_decode_region(int argc, char * const argv[])
937 {
938 	if (argc > 0) {
939 		if (0 == strcmp(*argv, "rw"))
940 			return EC_FLASH_REGION_RW;
941 		else if (0 == strcmp(*argv, "ro"))
942 			return EC_FLASH_REGION_RO;
943 
944 		debug("%s: Invalid region '%s'\n", __func__, *argv);
945 	} else {
946 		debug("%s: Missing region parameter\n", __func__);
947 	}
948 
949 	return -1;
950 }
951 
952 int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
953 {
954 	int flash_node, node;
955 
956 	node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
957 	if (node < 0) {
958 		debug("Failed to find chrome-ec node'\n");
959 		return -1;
960 	}
961 
962 	flash_node = fdt_subnode_offset(blob, node, "flash");
963 	if (flash_node < 0) {
964 		debug("Failed to find flash node\n");
965 		return -1;
966 	}
967 
968 	if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
969 				   &config->flash)) {
970 		debug("Failed to decode flash node in chrome-ec'\n");
971 		return -1;
972 	}
973 
974 	config->flash_erase_value = fdtdec_get_int(blob, flash_node,
975 						    "erase-value", -1);
976 	for (node = fdt_first_subnode(blob, flash_node); node >= 0;
977 	     node = fdt_next_subnode(blob, node)) {
978 		const char *name = fdt_get_name(blob, node, NULL);
979 		enum ec_flash_region region;
980 
981 		if (0 == strcmp(name, "ro")) {
982 			region = EC_FLASH_REGION_RO;
983 		} else if (0 == strcmp(name, "rw")) {
984 			region = EC_FLASH_REGION_RW;
985 		} else if (0 == strcmp(name, "wp-ro")) {
986 			region = EC_FLASH_REGION_WP_RO;
987 		} else {
988 			debug("Unknown EC flash region name '%s'\n", name);
989 			return -1;
990 		}
991 
992 		if (fdtdec_read_fmap_entry(blob, node, "reg",
993 					   &config->region[region])) {
994 			debug("Failed to decode flash region in chrome-ec'\n");
995 			return -1;
996 		}
997 	}
998 
999 	return 0;
1000 }
1001 
1002 /**
1003  * Perform a flash read or write command
1004  *
1005  * @param dev		CROS-EC device to read/write
1006  * @param is_write	1 do to a write, 0 to do a read
1007  * @param argc		Number of arguments
1008  * @param argv		Arguments (2 is region, 3 is address)
1009  * @return 0 for ok, 1 for a usage error or -ve for ec command error
1010  *	(negative EC_RES_...)
1011  */
1012 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1013 			 char * const argv[])
1014 {
1015 	uint32_t offset, size = -1U, region_size;
1016 	unsigned long addr;
1017 	char *endp;
1018 	int region;
1019 	int ret;
1020 
1021 	region = cros_ec_decode_region(argc - 2, argv + 2);
1022 	if (region == -1)
1023 		return 1;
1024 	if (argc < 4)
1025 		return 1;
1026 	addr = simple_strtoul(argv[3], &endp, 16);
1027 	if (*argv[3] == 0 || *endp != 0)
1028 		return 1;
1029 	if (argc > 4) {
1030 		size = simple_strtoul(argv[4], &endp, 16);
1031 		if (*argv[4] == 0 || *endp != 0)
1032 			return 1;
1033 	}
1034 
1035 	ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
1036 	if (ret) {
1037 		debug("%s: Could not read region info\n", __func__);
1038 		return ret;
1039 	}
1040 	if (size == -1U)
1041 		size = region_size;
1042 
1043 	ret = is_write ?
1044 		cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1045 		cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1046 	if (ret) {
1047 		debug("%s: Could not %s region\n", __func__,
1048 		      is_write ? "write" : "read");
1049 		return ret;
1050 	}
1051 
1052 	return 0;
1053 }
1054 
1055 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1056 {
1057 	struct cros_ec_dev *dev = last_dev;
1058 	const char *cmd;
1059 	int ret = 0;
1060 
1061 	if (argc < 2)
1062 		return CMD_RET_USAGE;
1063 
1064 	cmd = argv[1];
1065 	if (0 == strcmp("init", cmd)) {
1066 		ret = cros_ec_init(gd->fdt_blob, &dev);
1067 		if (ret) {
1068 			printf("Could not init cros_ec device (err %d)\n", ret);
1069 			return 1;
1070 		}
1071 		return 0;
1072 	}
1073 
1074 	/* Just use the last allocated device; there should be only one */
1075 	if (!last_dev) {
1076 		printf("No CROS-EC device available\n");
1077 		return 1;
1078 	}
1079 	if (0 == strcmp("id", cmd)) {
1080 		char id[MSG_BYTES];
1081 
1082 		if (cros_ec_read_id(dev, id, sizeof(id))) {
1083 			debug("%s: Could not read KBC ID\n", __func__);
1084 			return 1;
1085 		}
1086 		printf("%s\n", id);
1087 	} else if (0 == strcmp("info", cmd)) {
1088 		struct ec_response_cros_ec_info info;
1089 
1090 		if (cros_ec_info(dev, &info)) {
1091 			debug("%s: Could not read KBC info\n", __func__);
1092 			return 1;
1093 		}
1094 		printf("rows     = %u\n", info.rows);
1095 		printf("cols     = %u\n", info.cols);
1096 		printf("switches = %#x\n", info.switches);
1097 	} else if (0 == strcmp("curimage", cmd)) {
1098 		enum ec_current_image image;
1099 
1100 		if (cros_ec_read_current_image(dev, &image)) {
1101 			debug("%s: Could not read KBC image\n", __func__);
1102 			return 1;
1103 		}
1104 		printf("%d\n", image);
1105 	} else if (0 == strcmp("hash", cmd)) {
1106 		struct ec_response_vboot_hash hash;
1107 		int i;
1108 
1109 		if (cros_ec_read_hash(dev, &hash)) {
1110 			debug("%s: Could not read KBC hash\n", __func__);
1111 			return 1;
1112 		}
1113 
1114 		if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1115 			printf("type:    SHA-256\n");
1116 		else
1117 			printf("type:    %d\n", hash.hash_type);
1118 
1119 		printf("offset:  0x%08x\n", hash.offset);
1120 		printf("size:    0x%08x\n", hash.size);
1121 
1122 		printf("digest:  ");
1123 		for (i = 0; i < hash.digest_size; i++)
1124 			printf("%02x", hash.hash_digest[i]);
1125 		printf("\n");
1126 	} else if (0 == strcmp("reboot", cmd)) {
1127 		int region;
1128 		enum ec_reboot_cmd cmd;
1129 
1130 		if (argc >= 3 && !strcmp(argv[2], "cold"))
1131 			cmd = EC_REBOOT_COLD;
1132 		else {
1133 			region = cros_ec_decode_region(argc - 2, argv + 2);
1134 			if (region == EC_FLASH_REGION_RO)
1135 				cmd = EC_REBOOT_JUMP_RO;
1136 			else if (region == EC_FLASH_REGION_RW)
1137 				cmd = EC_REBOOT_JUMP_RW;
1138 			else
1139 				return CMD_RET_USAGE;
1140 		}
1141 
1142 		if (cros_ec_reboot(dev, cmd, 0)) {
1143 			debug("%s: Could not reboot KBC\n", __func__);
1144 			return 1;
1145 		}
1146 	} else if (0 == strcmp("events", cmd)) {
1147 		uint32_t events;
1148 
1149 		if (cros_ec_get_host_events(dev, &events)) {
1150 			debug("%s: Could not read host events\n", __func__);
1151 			return 1;
1152 		}
1153 		printf("0x%08x\n", events);
1154 	} else if (0 == strcmp("clrevents", cmd)) {
1155 		uint32_t events = 0x7fffffff;
1156 
1157 		if (argc >= 3)
1158 			events = simple_strtol(argv[2], NULL, 0);
1159 
1160 		if (cros_ec_clear_host_events(dev, events)) {
1161 			debug("%s: Could not clear host events\n", __func__);
1162 			return 1;
1163 		}
1164 	} else if (0 == strcmp("read", cmd)) {
1165 		ret = do_read_write(dev, 0, argc, argv);
1166 		if (ret > 0)
1167 			return CMD_RET_USAGE;
1168 	} else if (0 == strcmp("write", cmd)) {
1169 		ret = do_read_write(dev, 1, argc, argv);
1170 		if (ret > 0)
1171 			return CMD_RET_USAGE;
1172 	} else if (0 == strcmp("erase", cmd)) {
1173 		int region = cros_ec_decode_region(argc - 2, argv + 2);
1174 		uint32_t offset, size;
1175 
1176 		if (region == -1)
1177 			return CMD_RET_USAGE;
1178 		if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1179 			debug("%s: Could not read region info\n", __func__);
1180 			ret = -1;
1181 		} else {
1182 			ret = cros_ec_flash_erase(dev, offset, size);
1183 			if (ret) {
1184 				debug("%s: Could not erase region\n",
1185 				      __func__);
1186 			}
1187 		}
1188 	} else if (0 == strcmp("regioninfo", cmd)) {
1189 		int region = cros_ec_decode_region(argc - 2, argv + 2);
1190 		uint32_t offset, size;
1191 
1192 		if (region == -1)
1193 			return CMD_RET_USAGE;
1194 		ret = cros_ec_flash_offset(dev, region, &offset, &size);
1195 		if (ret) {
1196 			debug("%s: Could not read region info\n", __func__);
1197 		} else {
1198 			printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1199 					"RO" : "RW");
1200 			printf("Offset: %x\n", offset);
1201 			printf("Size:   %x\n", size);
1202 		}
1203 	} else if (0 == strcmp("vbnvcontext", cmd)) {
1204 		uint8_t block[EC_VBNV_BLOCK_SIZE];
1205 		char buf[3];
1206 		int i, len;
1207 		unsigned long result;
1208 
1209 		if (argc <= 2) {
1210 			ret = cros_ec_read_vbnvcontext(dev, block);
1211 			if (!ret) {
1212 				printf("vbnv_block: ");
1213 				for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1214 					printf("%02x", block[i]);
1215 				putc('\n');
1216 			}
1217 		} else {
1218 			/*
1219 			 * TODO(clchiou): Move this to a utility function as
1220 			 * cmd_spi might want to call it.
1221 			 */
1222 			memset(block, 0, EC_VBNV_BLOCK_SIZE);
1223 			len = strlen(argv[2]);
1224 			buf[2] = '\0';
1225 			for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1226 				if (i * 2 >= len)
1227 					break;
1228 				buf[0] = argv[2][i * 2];
1229 				if (i * 2 + 1 >= len)
1230 					buf[1] = '0';
1231 				else
1232 					buf[1] = argv[2][i * 2 + 1];
1233 				strict_strtoul(buf, 16, &result);
1234 				block[i] = result;
1235 			}
1236 			ret = cros_ec_write_vbnvcontext(dev, block);
1237 		}
1238 		if (ret) {
1239 			debug("%s: Could not %s VbNvContext\n", __func__,
1240 					argc <= 2 ?  "read" : "write");
1241 		}
1242 	} else if (0 == strcmp("test", cmd)) {
1243 		int result = cros_ec_test(dev);
1244 
1245 		if (result)
1246 			printf("Test failed with error %d\n", result);
1247 		else
1248 			puts("Test passed\n");
1249 	} else if (0 == strcmp("version", cmd)) {
1250 		struct ec_response_get_version *p;
1251 		char *build_string;
1252 
1253 		ret = cros_ec_read_version(dev, &p);
1254 		if (!ret) {
1255 			/* Print versions */
1256 			printf("RO version:    %1.*s\n",
1257 			       sizeof(p->version_string_ro),
1258 			       p->version_string_ro);
1259 			printf("RW version:    %1.*s\n",
1260 			       sizeof(p->version_string_rw),
1261 			       p->version_string_rw);
1262 			printf("Firmware copy: %s\n",
1263 				(p->current_image <
1264 					ARRAY_SIZE(ec_current_image_name) ?
1265 				ec_current_image_name[p->current_image] :
1266 				"?"));
1267 			ret = cros_ec_read_build_info(dev, &build_string);
1268 			if (!ret)
1269 				printf("Build info:    %s\n", build_string);
1270 		}
1271 	} else if (0 == strcmp("ldo", cmd)) {
1272 		uint8_t index, state;
1273 		char *endp;
1274 
1275 		if (argc < 3)
1276 			return CMD_RET_USAGE;
1277 		index = simple_strtoul(argv[2], &endp, 10);
1278 		if (*argv[2] == 0 || *endp != 0)
1279 			return CMD_RET_USAGE;
1280 		if (argc > 3) {
1281 			state = simple_strtoul(argv[3], &endp, 10);
1282 			if (*argv[3] == 0 || *endp != 0)
1283 				return CMD_RET_USAGE;
1284 			ret = cros_ec_set_ldo(dev, index, state);
1285 		} else {
1286 			ret = cros_ec_get_ldo(dev, index, &state);
1287 			if (!ret) {
1288 				printf("LDO%d: %s\n", index,
1289 					state == EC_LDO_STATE_ON ?
1290 					"on" : "off");
1291 			}
1292 		}
1293 
1294 		if (ret) {
1295 			debug("%s: Could not access LDO%d\n", __func__, index);
1296 			return ret;
1297 		}
1298 	} else {
1299 		return CMD_RET_USAGE;
1300 	}
1301 
1302 	if (ret < 0) {
1303 		printf("Error: CROS-EC command failed (error %d)\n", ret);
1304 		ret = 1;
1305 	}
1306 
1307 	return ret;
1308 }
1309 
1310 U_BOOT_CMD(
1311 	crosec,	5,	1,	do_cros_ec,
1312 	"CROS-EC utility command",
1313 	"init                Re-init CROS-EC (done on startup automatically)\n"
1314 	"crosec id                  Read CROS-EC ID\n"
1315 	"crosec info                Read CROS-EC info\n"
1316 	"crosec curimage            Read CROS-EC current image\n"
1317 	"crosec hash                Read CROS-EC hash\n"
1318 	"crosec reboot [rw | ro | cold]  Reboot CROS-EC\n"
1319 	"crosec events              Read CROS-EC host events\n"
1320 	"crosec clrevents [mask]    Clear CROS-EC host events\n"
1321 	"crosec regioninfo <ro|rw>  Read image info\n"
1322 	"crosec erase <ro|rw>       Erase EC image\n"
1323 	"crosec read <ro|rw> <addr> [<size>]   Read EC image\n"
1324 	"crosec write <ro|rw> <addr> [<size>]  Write EC image\n"
1325 	"crosec vbnvcontext [hexstring]        Read [write] VbNvContext from EC\n"
1326 	"crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1327 	"crosec test                run tests on cros_ec\n"
1328 	"crosec version             Read CROS-EC version"
1329 );
1330 #endif
1331