xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/vendor.c (revision 49fa68b0dbd5e2cf48ddcd537cf74590eeefd48f)
1 /*
2  * (C) Copyright 2008-2017 Fuzhou Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <malloc.h>
9 #include <asm/arch/vendor.h>
10 #include <boot_rkimg.h>
11 #include <nand.h>
12 #include <part.h>
13 #include <fdt_support.h>
14 #include <usbplug.h>
15 
16 /* tag for vendor check */
17 #define VENDOR_TAG		0x524B5644
18 /* The Vendor partition contains the number of Vendor blocks */
19 #define MTD_VENDOR_PART_NUM	1
20 #define NAND_VENDOR_PART_NUM	2
21 #define VENDOR_PART_NUM		4
22 /* align to 64 bytes */
23 #define VENDOR_BTYE_ALIGN	0x3F
24 #define VENDOR_BLOCK_SIZE	512
25 
26 #define PAGE_ALGIN_SIZE		(4096uL)
27 #define PAGE_ALGIN_MASK		(~(PAGE_ALGIN_SIZE - 1))
28 
29 /* --- Emmc define --- */
30 /* Starting address of the Vendor in memory. */
31 #define EMMC_VENDOR_PART_OFFSET		(1024 * 7)
32 /*
33  * The number of memory blocks used by each
34  * Vendor structure(128 * 512B = 64KB)
35  */
36 #define EMMC_VENDOR_PART_BLKS		128
37 /* The maximum number of items in each Vendor block */
38 #define EMMC_VENDOR_ITEM_NUM		126
39 
40 /* --- Spi Nand/SLC/MLC large capacity case define --- */
41 /* The Vendor partition contains the number of Vendor blocks */
42 #define NAND_VENDOR_PART_OFFSET		0
43 /*
44  * The number of memory blocks used by each
45  * Vendor structure(8 * 512B = 4KB)
46  */
47 #define NAND_VENDOR_PART_BLKS		128
48 /* The maximum number of items in each Vendor block */
49 #define NAND_VENDOR_ITEM_NUM		126
50 
51 /* --- Spi/Spi Nand/SLC/MLC small capacity case define --- */
52 /* The Vendor partition contains the number of Vendor blocks */
53 #define	FLASH_VENDOR_PART_OFFSET	8
54 /*
55  * The number of memory blocks used by each
56  * Vendor structure(8 * 512B = 4KB)
57  */
58 #define FLASH_VENDOR_PART_BLKS		8
59 /* The maximum number of items in each Vendor block */
60 #define FLASH_VENDOR_ITEM_NUM		62
61 
62 /* Vendor uinit test define */
63 int vendor_storage_test(void);
64 
65 struct vendor_hdr {
66 	u32	tag;
67 	u32	version;
68 	u16	next_index;
69 	u16	item_num;
70 	u16	free_offset; /* Free space offset */
71 	u16	free_size; /* Free space size */
72 };
73 
74 /*
75  * Different types of Flash vendor info are different.
76  * EMMC:EMMC_VENDOR_PART_BLKS * BLOCK_SIZE(512) = 64KB;
77  * Spi Nor/Spi Nand/SLC/MLC: FLASH_VENDOR_PART_BLKS *
78  * BLOCK_SIZE(512) = 4KB.
79  * hash: For future expansion.
80  * version2: Together with hdr->version, it is used to
81  * ensure the current Vendor block content integrity.
82  *   (version2 == hdr->version):Data valid;
83  *   (version2 != hdr->version):Data invalid.
84  */
85 struct vendor_info {
86 	struct vendor_hdr *hdr;
87 	struct vendor_item *item;
88 	u8 *data;
89 	u32 *hash;
90 	u32 *version2;
91 };
92 
93 struct mtd_flash_info {
94 	u32 part_offset;
95 	u32 part_size;
96 	u32 blk_offset;
97 	u32 page_offset;
98 	u32 version;
99 	u32 ops_size;
100 	u32 blk_size;
101 };
102 
103 /*
104  * Calculate the offset of each field for emmc.
105  * Emmc vendor info size: 64KB
106  */
107 #define EMMC_VENDOR_INFO_SIZE	(EMMC_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE)
108 #define EMMC_VENDOR_DATA_OFFSET	(sizeof(struct vendor_hdr) + EMMC_VENDOR_ITEM_NUM * sizeof(struct vendor_item))
109 #define EMMC_VENDOR_HASH_OFFSET (EMMC_VENDOR_INFO_SIZE - 8)
110 #define EMMC_VENDOR_VERSION2_OFFSET (EMMC_VENDOR_INFO_SIZE - 4)
111 
112 /*
113  * Calculate the offset of each field for spi nand/slc/mlc large capacity case.
114  * Flash vendor info size: 4KB
115  */
116 #define NAND_VENDOR_INFO_SIZE	(NAND_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE)
117 #define NAND_VENDOR_DATA_OFFSET	(sizeof(struct vendor_hdr) + NAND_VENDOR_ITEM_NUM * sizeof(struct vendor_item))
118 #define NAND_VENDOR_HASH_OFFSET (NAND_VENDOR_INFO_SIZE - 8)
119 #define NAND_VENDOR_VERSION2_OFFSET (NAND_VENDOR_INFO_SIZE - 4)
120 
121 /*
122  * Calculate the offset of each field for spi nor/spi nand/slc/mlc large small capacity case.
123  * Flash vendor info size: 4KB
124  */
125 #define FLASH_VENDOR_INFO_SIZE	(FLASH_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE)
126 #define FLASH_VENDOR_DATA_OFFSET (sizeof(struct vendor_hdr) + FLASH_VENDOR_ITEM_NUM * sizeof(struct vendor_item))
127 #define FLASH_VENDOR_HASH_OFFSET (FLASH_VENDOR_INFO_SIZE - 8)
128 #define FLASH_VENDOR_VERSION2_OFFSET (FLASH_VENDOR_INFO_SIZE - 4)
129 
130 /* vendor info */
131 static struct vendor_info vendor_info;
132 /* The storage type of the device */
133 static int bootdev_type;
134 
135 #ifdef CONFIG_MTD_BLK
136 static struct mtd_flash_info s_flash_info;
137 static const char *vendor_mtd_name = "vnvm";
138 #endif
139 
140 /* vendor private read write ops*/
141 static	int (*_flash_read)(struct blk_desc *dev_desc,
142 			   u32 sec,
143 			   u32 n_sec,
144 			   void *buffer);
145 static	int (*_flash_write)(struct blk_desc *dev_desc,
146 			    u32 sec,
147 			    u32 n_sec,
148 			    void *buffer);
149 
150 int flash_vendor_dev_ops_register(int (*read)(struct blk_desc *dev_desc,
151 					      u32 sec,
152 					      u32 n_sec,
153 					      void *p_data),
154 				  int (*write)(struct blk_desc *dev_desc,
155 					       u32 sec,
156 					       u32 n_sec,
157 					       void *p_data))
158 {
159 	if (!_flash_read) {
160 		_flash_read = read;
161 		_flash_write = write;
162 		return 0;
163 	}
164 
165 	return -EPERM;
166 }
167 
168 #ifdef CONFIG_MTD_BLK
169 static int mtd_vendor_storage_init(struct blk_desc *dev_desc)
170 {
171 	struct mtd_info *mtd = (struct mtd_info *)dev_desc->bdev->priv;
172 	disk_partition_t vnvm_part_info;
173 	void *buf = vendor_info.hdr;
174 	int ret, offset;
175 	int part_num, bad_block_size;
176 
177 	memset(&vnvm_part_info, 0x0, sizeof(vnvm_part_info));
178 	part_num = part_get_info_by_name(dev_desc, vendor_mtd_name, &vnvm_part_info);
179 	if (part_num < 0)
180 		return -EIO;
181 
182 	s_flash_info.part_offset = (u32)vnvm_part_info.start;
183 	s_flash_info.part_size = (u32)vnvm_part_info.size;
184 	s_flash_info.page_offset = 0;
185 	s_flash_info.blk_offset = 0;
186 	s_flash_info.version = 0;
187 	/* SPI Nor unified to Support 64KB erase block */
188 	if (dev_desc->devnum == BLK_MTD_SPI_NOR)
189 		s_flash_info.blk_size = 0x80;
190 	else
191 		s_flash_info.blk_size = mtd->erasesize >> 9;
192 	s_flash_info.ops_size = roundup(FLASH_VENDOR_INFO_SIZE, mtd->writesize) >> 9;
193 
194 	/* scan bad block and calculate the real size can be used */
195 	bad_block_size = 0;
196 	for (offset = 0; offset < s_flash_info.part_size; offset += s_flash_info.blk_size) {
197 		if (mtd_block_isbad(mtd, (s_flash_info.part_offset + offset) << 9))
198 			bad_block_size += s_flash_info.blk_size;
199 	}
200 	s_flash_info.part_size -= bad_block_size;
201 
202 	for (offset = 0; offset < s_flash_info.part_size; offset += s_flash_info.blk_size) {
203 		ret = blk_dread(dev_desc, s_flash_info.part_offset + offset,
204 				FLASH_VENDOR_INFO_SIZE >> 9,
205 				(u8 *)buf);
206 		debug("%s: read %x version = %x\n", __func__,
207 		      s_flash_info.part_offset + offset,
208 		      vendor_info.hdr->version);
209 		if (ret == (FLASH_VENDOR_INFO_SIZE >> 9) && vendor_info.hdr->tag == VENDOR_TAG &&
210 		    vendor_info.hdr->version == *vendor_info.version2) {
211 			if (vendor_info.hdr->version > s_flash_info.version) {
212 				s_flash_info.version = vendor_info.hdr->version;
213 				s_flash_info.blk_offset = offset;
214 			}
215 		}
216 	}
217 
218 	debug("%s: s_flash_info.version = %x %x\n", __func__, s_flash_info.version, s_flash_info.blk_offset);
219 	if (s_flash_info.version) {
220 		for (offset = s_flash_info.blk_size  - s_flash_info.ops_size;
221 		     offset >= 0;
222 		     offset -= s_flash_info.ops_size) {
223 			ret = blk_dread(dev_desc, s_flash_info.part_offset +
224 					s_flash_info.blk_offset + offset,
225 					1,
226 					(u8 *)buf);
227 
228 			/* the page is not programmed */
229 			if (ret == 1 && vendor_info.hdr->tag == 0xFFFFFFFF)
230 				continue;
231 
232 			/* point to the next free page */
233 			if (s_flash_info.page_offset < offset)
234 				s_flash_info.page_offset = offset + s_flash_info.ops_size;
235 
236 			if (ret != 1 || vendor_info.hdr->tag != VENDOR_TAG)
237 				continue;
238 			ret = blk_dread(dev_desc, s_flash_info.part_offset +
239 					s_flash_info.blk_offset + offset,
240 					FLASH_VENDOR_INFO_SIZE >> 9,
241 					(u8 *)buf);
242 			debug("%s: read %x version = %x\n", __func__,
243 			      s_flash_info.part_offset + s_flash_info.blk_offset  + offset,
244 			      vendor_info.hdr->version);
245 
246 			if (ret == (FLASH_VENDOR_INFO_SIZE >> 9)  && vendor_info.hdr->tag == VENDOR_TAG &&
247 			    vendor_info.hdr->version == *vendor_info.version2) {
248 				s_flash_info.version = vendor_info.hdr->version;
249 				break;
250 			}
251 		}
252 	} else {
253 		memset((u8 *)vendor_info.hdr, 0, FLASH_VENDOR_INFO_SIZE);
254 		vendor_info.hdr->version = 1;
255 		vendor_info.hdr->tag = VENDOR_TAG;
256 		vendor_info.hdr->free_size =
257 			((u32)(size_t)vendor_info.hash
258 			- (u32)(size_t)vendor_info.data);
259 		*vendor_info.version2 = vendor_info.hdr->version;
260 	}
261 
262 	return 0;
263 }
264 
265 static int mtd_vendor_write(struct blk_desc *dev_desc,
266 			    u32 sec,
267 			    u32 n_sec,
268 			    void *buf)
269 {
270 	int ret, count = 0, err = 0;
271 
272 re_write:
273 	debug("[Vendor INFO]:%s page_offset=0x%x count = %x\n", __func__, s_flash_info.part_offset +
274 	      s_flash_info.blk_offset + s_flash_info.page_offset, count);
275 	if (s_flash_info.page_offset >= s_flash_info.blk_size) {
276 		s_flash_info.blk_offset += s_flash_info.blk_size;
277 		if (s_flash_info.blk_offset >= s_flash_info.part_size)
278 			s_flash_info.blk_offset = 0;
279 		s_flash_info.page_offset = 0;
280 		/*
281 		 * The spi NOR driver only erase 4KB while write data, and here need to
282 		 * erase one block for vendor storage request.
283 		 */
284 		blk_derase(dev_desc, s_flash_info.part_offset + s_flash_info.blk_offset, s_flash_info.blk_size);
285 	}
286 
287 	dev_desc->op_flag |= BLK_MTD_CONT_WRITE;
288 	ret = blk_dwrite(dev_desc, s_flash_info.part_offset +
289 			 s_flash_info.blk_offset + s_flash_info.page_offset,
290 			 FLASH_VENDOR_INFO_SIZE >> 9,
291 			 (u8 *)buf);
292 	dev_desc->op_flag &= ~(BLK_MTD_CONT_WRITE);
293 
294 	s_flash_info.page_offset += s_flash_info.ops_size;
295 	if (ret != (FLASH_VENDOR_INFO_SIZE >> 9)) {
296 		err++;
297 		if (err > 3)
298 			return -EIO;
299 		goto re_write;
300 	}
301 
302 	count++;
303 	/* write 2 copies for reliability */
304 	if (count < 2)
305 		goto re_write;
306 
307 	return ret;
308 }
309 #endif
310 
311 /**********************************************************/
312 /*              vendor API implementation                 */
313 /**********************************************************/
314 static int vendor_ops(u8 *buffer, u32 addr, u32 n_sec, int write)
315 {
316 	struct blk_desc *dev_desc;
317 	unsigned int lba = 0;
318 	int ret = 0;
319 
320 	dev_desc = rockchip_get_bootdev();
321 	if (!dev_desc) {
322 		printf("%s: dev_desc is NULL!\n", __func__);
323 		return -ENODEV;
324 	}
325 
326 	if (dev_desc->if_type == IF_TYPE_NVME || dev_desc->if_type == IF_TYPE_SCSI) {
327 		dev_desc = blk_get_devnum_by_type(IF_TYPE_MTD, BLK_MTD_SPI_NOR);
328 		if (!dev_desc) {
329 			printf("%s: dev_desc is NULL!\n", __func__);
330 			return -ENODEV;
331 		}
332 	}
333 
334 	/* Get the offset address according to the device type */
335 	switch (dev_desc->if_type) {
336 	case IF_TYPE_MMC:
337 		/*
338 		 * The location of VendorStorage in Flash is shown in the
339 		 * following figure. The starting address of the VendorStorage
340 		 * partition offset is 3.5MB(EMMC_VENDOR_PART_OFFSET*BLOCK_SIZE(512)),
341 		 * and the partition size is 256KB.
342 		 * ----------------------------------------------------
343 		 * |   3.5MB    |  VendorStorage  |                   |
344 		 * ----------------------------------------------------
345 		 */
346 		lba = EMMC_VENDOR_PART_OFFSET;
347 		debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba);
348 		break;
349 	case IF_TYPE_RKNAND:
350 	case IF_TYPE_SPINAND:
351 		/*
352 		 * The location of VendorStorage in Flash is shown in the
353 		 * following figure. The starting address of the VendorStorage
354 		 * partition offset is 0KB in FTL vendor block,
355 		 * and the partition size is 128KB.
356 		 * ----------------------------------------------------
357 		 * |  VendorStorage  |                     |
358 		 * ----------------------------------------------------
359 		 */
360 		lba = NAND_VENDOR_PART_OFFSET;
361 		debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba);
362 		break;
363 	case IF_TYPE_SPINOR:
364 		/*
365 		 * The location of VendorStorage in Flash is shown in the
366 		 * following figure. The starting address of the VendorStorage
367 		 * partition offset is 4KB (FLASH_VENDOR_PART_OFFSET * BLOCK_SIZE),
368 		 * and the partition size is 16KB.
369 		 * ----------------------------------------------------
370 		 * |   4KB    |  VendorStorage  |                     |
371 		 * ----------------------------------------------------
372 		 */
373 		lba = FLASH_VENDOR_PART_OFFSET;
374 		debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba);
375 		break;
376 #ifdef CONFIG_MTD_BLK
377 	case IF_TYPE_MTD:
378 		/*
379 		 * The location of VendorStorage in NAND FLASH or SPI NAND partition "vnvm"
380 		 * is shown in the following figure. The partition size is at least  4
381 		 * NAND FLASH blocks.
382 		 * ----------------------------------------------------
383 		 * |   .....    |  vnvm  |  .......                   |
384 		 * ----------------------------------------------------
385 		 */
386 		lba = 0;
387 		break;
388 #endif
389 	default:
390 		printf("[Vendor ERROR]:Boot device type is invalid!\n");
391 		return -ENODEV;
392 	}
393 	if (write) {
394 		if (_flash_write)
395 			ret = _flash_write(dev_desc, lba + addr, n_sec, buffer);
396 		else
397 			ret = blk_dwrite(dev_desc, lba + addr, n_sec, buffer);
398 	} else {
399 		if (_flash_read)
400 			ret = _flash_read(dev_desc, lba + addr, n_sec, buffer);
401 		else
402 			ret = blk_dread(dev_desc, lba + addr, n_sec, buffer);
403 	}
404 
405 	debug("[Vendor INFO]:op=%s, ret=%d\n", write ? "write" : "read", ret);
406 
407 	return ret;
408 }
409 
410 /*
411  * The VendorStorage partition is divided into four parts
412  * (vendor 0-3) and its structure is shown in the following figure.
413  * The init function is used to select the latest and valid vendor.
414  *
415  * |******************** FLASH ********************|
416  * -------------------------------------------------
417  * |  vendor0  |  vendor1  |  vendor2  |  vendor3  |
418  * -------------------------------------------------
419  * Notices:
420  *   1. "version" and "version2" are used to verify that the vendor
421  *      is valid (equal is valid).
422  *   2. the "version" value is larger, indicating that the current
423  *      verndor data is new.
424  */
425 int vendor_storage_init(void)
426 {
427 	int ret = 0;
428 	int ret_size;
429 	u8 *buffer;
430 	u32 size, i;
431 	u32 max_ver = 0;
432 	u32 max_index = 0;
433 	u16 data_offset, hash_offset, part_num;
434 	u16 version2_offset, part_size;
435 	struct blk_desc *dev_desc;
436 
437 	dev_desc = rockchip_get_bootdev();
438 	if (!dev_desc) {
439 		printf("[Vendor ERROR]:Invalid boot device type(%d)\n",
440 		       bootdev_type);
441 		return -ENODEV;
442 	}
443 
444 	if (dev_desc->if_type == IF_TYPE_NVME || dev_desc->if_type == IF_TYPE_SCSI) {
445 		dev_desc = blk_get_devnum_by_type(IF_TYPE_MTD, BLK_MTD_SPI_NOR);
446 		if (!dev_desc) {
447 			printf("%s: dev_desc is NULL!\n", __func__);
448 			return -ENODEV;
449 		}
450 	}
451 
452 	switch (dev_desc->if_type) {
453 	case IF_TYPE_MMC:
454 		size = EMMC_VENDOR_INFO_SIZE;
455 		part_size = EMMC_VENDOR_PART_BLKS;
456 		data_offset = EMMC_VENDOR_DATA_OFFSET;
457 		hash_offset = EMMC_VENDOR_HASH_OFFSET;
458 		version2_offset = EMMC_VENDOR_VERSION2_OFFSET;
459 		part_num = VENDOR_PART_NUM;
460 		break;
461 	case IF_TYPE_RKNAND:
462 	case IF_TYPE_SPINAND:
463 		size = NAND_VENDOR_INFO_SIZE;
464 		part_size = NAND_VENDOR_PART_BLKS;
465 		data_offset = NAND_VENDOR_DATA_OFFSET;
466 		hash_offset = NAND_VENDOR_HASH_OFFSET;
467 		version2_offset = NAND_VENDOR_VERSION2_OFFSET;
468 		part_num = NAND_VENDOR_PART_NUM;
469 		break;
470 	case IF_TYPE_SPINOR:
471 		size = FLASH_VENDOR_INFO_SIZE;
472 		part_size = FLASH_VENDOR_PART_BLKS;
473 		data_offset = FLASH_VENDOR_DATA_OFFSET;
474 		hash_offset = FLASH_VENDOR_HASH_OFFSET;
475 		version2_offset = FLASH_VENDOR_VERSION2_OFFSET;
476 		part_num = VENDOR_PART_NUM;
477 		break;
478 #ifdef CONFIG_MTD_BLK
479 	case IF_TYPE_MTD:
480 		size = FLASH_VENDOR_INFO_SIZE;
481 		part_size = FLASH_VENDOR_PART_BLKS;
482 		data_offset = FLASH_VENDOR_DATA_OFFSET;
483 		hash_offset = FLASH_VENDOR_HASH_OFFSET;
484 		version2_offset = FLASH_VENDOR_VERSION2_OFFSET;
485 		part_num = MTD_VENDOR_PART_NUM;
486 		_flash_write = mtd_vendor_write;
487 		break;
488 #endif
489 	default:
490 		debug("[Vendor ERROR]:Boot device type is invalid!\n");
491 		ret = -ENODEV;
492 		break;
493 	}
494 	/* Invalid bootdev type */
495 	if (ret)
496 		return ret;
497 
498 	/* Initialize */
499 	bootdev_type = dev_desc->if_type;
500 
501 	/* Always use, no need to release, align to page size for kerenl reserved memory */
502 	buffer = (u8 *)memalign(PAGE_ALGIN_SIZE, size);
503 	if (!buffer) {
504 		printf("[Vendor ERROR]:Malloc failed!\n");
505 		ret = -ENOMEM;
506 		goto out;
507 	}
508 
509 	/* Pointer initialization */
510 	vendor_info.hdr = (struct vendor_hdr *)buffer;
511 	vendor_info.item = (struct vendor_item *)(buffer + sizeof(struct vendor_hdr));
512 	vendor_info.data = buffer + data_offset;
513 	vendor_info.hash = (u32 *)(buffer + hash_offset);
514 	vendor_info.version2 = (u32 *)(buffer + version2_offset);
515 
516 #ifdef CONFIG_MTD_BLK
517 	if (dev_desc->if_type == IF_TYPE_MTD) {
518 		ret = mtd_vendor_storage_init(dev_desc);
519 		goto out;
520 	}
521 #endif
522 
523 	/* Find valid and up-to-date one from (vendor0 - vendor3) */
524 	for (i = 0; i < part_num; i++) {
525 		ret_size = vendor_ops((u8 *)vendor_info.hdr,
526 				      part_size * i, part_size, 0);
527 		if (ret_size != part_size) {
528 			ret = -EIO;
529 			goto out;
530 		}
531 
532 		if ((vendor_info.hdr->tag == VENDOR_TAG) &&
533 		    (*(vendor_info.version2) == vendor_info.hdr->version)) {
534 			if (max_ver < vendor_info.hdr->version) {
535 				max_index = i;
536 				max_ver = vendor_info.hdr->version;
537 			}
538 		}
539 	}
540 
541 	if (max_ver) {
542 		debug("[Vendor INFO]:max_ver=%d, vendor_id=%d.\n", max_ver, max_index);
543 		/*
544 		 * Keep vendor_info the same as the largest
545 		 * version of vendor
546 		 */
547 		if (max_index != (part_num - 1)) {
548 			ret_size = vendor_ops((u8 *)vendor_info.hdr,
549 					       part_size * max_index, part_size, 0);
550 			if (ret_size != part_size) {
551 				ret = -EIO;
552 				goto out;
553 			}
554 		}
555 	} else {
556 		debug("[Vendor INFO]:Reset vendor info...\n");
557 		memset((u8 *)vendor_info.hdr, 0, size);
558 		vendor_info.hdr->version = 1;
559 		vendor_info.hdr->tag = VENDOR_TAG;
560 		/* data field length */
561 		vendor_info.hdr->free_size =
562 			((u32)(size_t)vendor_info.hash
563 			- (u32)(size_t)vendor_info.data);
564 		*(vendor_info.version2) = vendor_info.hdr->version;
565 	}
566 	debug("[Vendor INFO]:ret=%d.\n", ret);
567 
568 out:
569 	if (ret)
570 		bootdev_type = 0;
571 
572 	return ret;
573 }
574 
575 void vendor_storage_fixup(void *blob)
576 {
577 	unsigned long size;
578 	unsigned long start;
579 	int offset;
580 
581 	/* init vendor storage */
582 	if (!bootdev_type) {
583 		if (vendor_storage_init() < 0)
584 			return;
585 	}
586 
587 	offset = fdt_node_offset_by_compatible(blob, 0, "rockchip,vendor-storage-rm");
588 	if (offset >= 0) {
589 		start = (unsigned long)vendor_info.hdr;
590 		size = (unsigned long)((void *)vendor_info.version2 - (void *)vendor_info.hdr);
591 		size += 4;
592 		fdt_update_reserved_memory(blob, "rockchip,vendor-storage-rm",
593 					   (u64)start,
594 					   (u64)size);
595 	}
596 }
597 
598 /*
599  * @id: item id, first 4 id is occupied:
600  *	VENDOR_SN_ID
601  *	VENDOR_WIFI_MAC_ID
602  *	VENDOR_LAN_MAC_ID
603  *	VENDOR_BLUETOOTH_ID
604  * @pbuf: read data buffer;
605  * @size: read bytes;
606  *
607  * return: bytes equal to @size is success, other fail;
608  */
609 int vendor_storage_read(u16 id, void *pbuf, u16 size)
610 {
611 	int ret = 0;
612 	u32 i;
613 	u16 offset;
614 	struct vendor_item *item;
615 
616 	/* init vendor storage */
617 	if (!bootdev_type) {
618 		ret = vendor_storage_init();
619 		if (ret < 0)
620 			return ret;
621 	}
622 
623 	item = vendor_info.item;
624 	for (i = 0; i < vendor_info.hdr->item_num; i++) {
625 		if ((item + i)->id == id) {
626 			debug("[Vendor INFO]:Find the matching item, id=%d\n", id);
627 			/* Correct the size value */
628 			if (size > (item + i)->size)
629 				size = (item + i)->size;
630 			offset = (item + i)->offset;
631 			memcpy(pbuf, (vendor_info.data + offset), size);
632 			return size;
633 		}
634 	}
635 	debug("[Vendor ERROR]:No matching item, id=%d\n", id);
636 
637 	return -EINVAL;
638 }
639 
640 /*
641  * @id: item id, first 4 id is occupied:
642  *	VENDOR_SN_ID
643  *	VENDOR_WIFI_MAC_ID
644  *	VENDOR_LAN_MAC_ID
645  *	VENDOR_BLUETOOTH_ID
646  * @pbuf: write data buffer;
647  * @size: write bytes;
648  *
649  * return: bytes equal to @size is success, other fail;
650  */
651 int vendor_storage_write(u16 id, void *pbuf, u16 size)
652 {
653 	u32 i, j, next_index, align_size, alloc_size, next_size;
654 	u16 part_size, max_item_num, offset, part_num;
655 	struct vendor_item *item;
656 	int cnt, ret = 0;
657 
658 	/* init vendor storage */
659 	if (!bootdev_type) {
660 		ret = vendor_storage_init();
661 		if (ret < 0)
662 			return ret;
663 	}
664 
665 	switch (bootdev_type) {
666 	case IF_TYPE_MMC:
667 		part_size = EMMC_VENDOR_PART_BLKS;
668 		max_item_num = EMMC_VENDOR_ITEM_NUM;
669 		part_num = VENDOR_PART_NUM;
670 		break;
671 	case IF_TYPE_RKNAND:
672 	case IF_TYPE_SPINAND:
673 		part_size = NAND_VENDOR_PART_BLKS;
674 		max_item_num = NAND_VENDOR_ITEM_NUM;
675 		part_num = NAND_VENDOR_PART_NUM;
676 		break;
677 	case IF_TYPE_SPINOR:
678 		part_size = FLASH_VENDOR_PART_BLKS;
679 		max_item_num = FLASH_VENDOR_ITEM_NUM;
680 		part_num = VENDOR_PART_NUM;
681 		break;
682 #ifdef CONFIG_MTD_BLK
683 	case IF_TYPE_MTD:
684 		part_size = FLASH_VENDOR_PART_BLKS;
685 		max_item_num = FLASH_VENDOR_ITEM_NUM;
686 		part_num = MTD_VENDOR_PART_NUM;
687 		break;
688 #endif
689 	default:
690 		ret = -ENODEV;
691 		break;
692 	}
693 	/* Invalid bootdev? */
694 	if (ret < 0)
695 		return ret;
696 
697 	next_index = vendor_info.hdr->next_index;
698 	/* algin to 64 bytes*/
699 	align_size = (size + VENDOR_BTYE_ALIGN) & (~VENDOR_BTYE_ALIGN);
700 	if (size > align_size)
701 		return -EINVAL;
702 
703 	item = vendor_info.item;
704 	/* If item already exist, update the item data */
705 	for (i = 0; i < vendor_info.hdr->item_num; i++) {
706 		if ((item + i)->id == id) {
707 			alloc_size = ((item + i)->size + VENDOR_BTYE_ALIGN) & (~VENDOR_BTYE_ALIGN);
708 			if (size > alloc_size) {
709 				if (vendor_info.hdr->free_size < align_size)
710 					return -EINVAL;
711 				debug("[Vendor INFO]:Find the matching item, id=%d and resize\n", id);
712 				offset = (item + i)->offset;
713 				for (j = i; j < vendor_info.hdr->item_num - 1; j++) {
714 					(item + j)->id = (item + j + 1)->id;
715 					(item + j)->size = (item + j + 1)->size;
716 					(item + j)->offset = offset;
717 
718 					next_size = ((item + j + 1)->size + VENDOR_BTYE_ALIGN) & (~VENDOR_BTYE_ALIGN);
719 					memcpy((vendor_info.data + offset),
720 					       (vendor_info.data + (item + j + 1)->offset),
721 					       next_size);
722 					offset += next_size;
723 				}
724 				(item + j)->id = id;
725 				(item + j)->offset = offset;
726 				(item + j)->size = size;
727 				memcpy((vendor_info.data + offset), pbuf, size);
728 				vendor_info.hdr->free_offset = offset + align_size;
729 				vendor_info.hdr->free_size -= align_size - alloc_size;
730 			} else {
731 				debug("[Vendor INFO]:Find the matching item, id=%d\n", id);
732 				offset = (item + i)->offset;
733 				memcpy((vendor_info.data + offset), pbuf, size);
734 				(item + i)->size = size;
735 			}
736 			vendor_info.hdr->version++;
737 			*(vendor_info.version2) = vendor_info.hdr->version;
738 			vendor_info.hdr->next_index++;
739 			if (vendor_info.hdr->next_index >= part_num)
740 				vendor_info.hdr->next_index = 0;
741 			cnt = vendor_ops((u8 *)vendor_info.hdr, part_size * next_index, part_size, 1);
742 			return (cnt == part_size) ? size : -EIO;
743 		}
744 	}
745 	/*
746 	 * If item does not exist, and free size is enough,
747 	 * creat a new one
748 	 */
749 	if ((vendor_info.hdr->item_num < max_item_num) &&
750 	    (vendor_info.hdr->free_size >= align_size)) {
751 		debug("[Vendor INFO]:Create new Item, id=%d\n", id);
752 		item = vendor_info.item + vendor_info.hdr->item_num;
753 		item->id = id;
754 		item->offset = vendor_info.hdr->free_offset;
755 		item->size = size;
756 
757 		vendor_info.hdr->free_offset += align_size;
758 		vendor_info.hdr->free_size -= align_size;
759 		memcpy((vendor_info.data + item->offset), pbuf, size);
760 		vendor_info.hdr->item_num++;
761 		vendor_info.hdr->version++;
762 		vendor_info.hdr->next_index++;
763 		*(vendor_info.version2) = vendor_info.hdr->version;
764 		if (vendor_info.hdr->next_index >= part_num)
765 			vendor_info.hdr->next_index = 0;
766 
767 		cnt = vendor_ops((u8 *)vendor_info.hdr, part_size * next_index, part_size, 1);
768 		return (cnt == part_size) ? size : -EIO;
769 	}
770 	debug("[Vendor ERROR]:Vendor has no space left!\n");
771 
772 	return -ENOMEM;
773 }
774 
775 /**********************************************************/
776 /*              vendor API uinit test                      */
777 /**********************************************************/
778 /* Reset the vendor storage space to the initial state */
779 static void vendor_test_reset(void)
780 {
781 	u16 i, part_size, part_num;
782 	u32 size;
783 
784 	switch (bootdev_type) {
785 	case IF_TYPE_MMC:
786 		size = EMMC_VENDOR_INFO_SIZE;
787 		part_size = EMMC_VENDOR_PART_BLKS;
788 		part_num = VENDOR_PART_NUM;
789 		break;
790 	case IF_TYPE_RKNAND:
791 	case IF_TYPE_SPINAND:
792 		size = NAND_VENDOR_INFO_SIZE;
793 		part_size = NAND_VENDOR_PART_BLKS;
794 		part_num = NAND_VENDOR_PART_NUM;
795 		break;
796 	case IF_TYPE_SPINOR:
797 		size = FLASH_VENDOR_INFO_SIZE;
798 		part_size = FLASH_VENDOR_PART_BLKS;
799 		part_num = VENDOR_PART_NUM;
800 		break;
801 	default:
802 		size = 0;
803 		part_size = 0;
804 		break;
805 	}
806 	/* Invalid bootdev? */
807 	if (!size)
808 		return;
809 
810 	memset((u8 *)vendor_info.hdr, 0, size);
811 	vendor_info.hdr->version = 1;
812 	vendor_info.hdr->tag = VENDOR_TAG;
813 	/* data field length */
814 	vendor_info.hdr->free_size = (unsigned long)vendor_info.hash -
815 				     (unsigned long)vendor_info.data;
816 	*(vendor_info.version2) = vendor_info.hdr->version;
817 	/* write to flash. */
818 	for (i = 0; i < part_num; i++)
819 		vendor_ops((u8 *)vendor_info.hdr, part_size * i, part_size, 1);
820 }
821 
822 /*
823  * A total of four tests
824  * 1.All items test.
825  * 2.Overrides the maximum number of items test.
826  * 3.Single Item memory overflow test.
827  * 4.Total memory overflow test.
828  */
829 int vendor_storage_test(void)
830 {
831 	u16 id, size, j, item_num;
832 	u32 total_size;
833 	u8 *buffer = NULL;
834 	int ret = 0;
835 
836 	if (!bootdev_type) {
837 		ret = vendor_storage_init();
838 		if (ret) {
839 			printf("%s: vendor storage init failed, ret=%d\n",
840 			       __func__, ret);
841 			return ret;
842 		}
843 	}
844 
845 	/*
846 	 * Calculate the maximum number of items and the maximum
847 	 * allocable memory for each item.
848 	 */
849 	switch (bootdev_type) {
850 	case IF_TYPE_MMC:
851 		item_num = EMMC_VENDOR_ITEM_NUM;
852 		total_size = (unsigned long)vendor_info.hash -
853 			     (unsigned long)vendor_info.data;
854 		size = total_size / item_num;
855 		break;
856 	case IF_TYPE_RKNAND:
857 	case IF_TYPE_SPINAND:
858 		item_num = NAND_VENDOR_ITEM_NUM;
859 		total_size = (unsigned long)vendor_info.hash -
860 			     (unsigned long)vendor_info.data;
861 		size = total_size / item_num;
862 		break;
863 	case IF_TYPE_SPINOR:
864 	case IF_TYPE_MTD:
865 		item_num = FLASH_VENDOR_ITEM_NUM;
866 		total_size = (unsigned long)vendor_info.hash -
867 			     (unsigned long)vendor_info.data;
868 		size = total_size / item_num;
869 		break;
870 	default:
871 		item_num = 0;
872 		total_size = 0;
873 		size = 0;
874 		break;
875 	}
876 	/* Invalid bootdev? */
877 	if (!total_size)
878 		return -ENODEV;
879 	/* 64 bytes are aligned and rounded down */
880 	if (size > 64)
881 		size = (size / 64) * 64;
882 	/* malloc memory */
883 	buffer = (u8 *)malloc(size);
884 	if (!buffer) {
885 		printf("[Vendor Test]:Malloc failed(size=%d)!\n", size);
886 		return -ENOMEM;
887 	}
888 	printf("[Vendor Test]:Test Start...\n");
889 	printf("[Vendor Test]:Before Test, Vendor Resetting.\n");
890 	if (bootdev_type != IF_TYPE_MTD)
891 		vendor_test_reset();
892 
893 	/* FIRST TEST: test all items can be used correctly */
894 	printf("[Vendor Test]:<All Items Used> Test Start...\n");
895 	printf("[Vendor Test]:item_num=%d, size=%d.\n", item_num, size);
896 	/*
897 	 * Write data, then read the data, and compare the
898 	 * data consistency
899 	 */
900 	for (id = 0; id < item_num; id++) {
901 		memset(buffer, id, size);
902 		ret = vendor_storage_write(id, buffer, size);
903 		if (ret < 0) {
904 			printf("[Vendor Test]:vendor write failed(id=%d)!\n", id);
905 			free(buffer);
906 			return ret;
907 		}
908 	}
909 	/* Read data */
910 	for (id = 0; id < item_num; id++) {
911 		memset(buffer, 0, size);
912 		ret = vendor_storage_read(id, buffer, size);
913 		if (ret < 0) {
914 			printf("[Vendor Test]:vendor read failed(id=%d)!\n", id);
915 			free(buffer);
916 			return ret;
917 		}
918 		/* check data Correctness */
919 		for (j = 0; j < size; j++) {
920 			if (*(buffer + j) != id) {
921 				printf("[Vendor Test]:Unexpected error occurs(id=%d)\n", id);
922 				printf("the data content is:\n");
923 				print_buffer(0, buffer, 1, size, 16);
924 
925 				free(buffer);
926 				return -1;
927 			}
928 		}
929 		debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size);
930 	}
931 	printf("[Vendor Test]:<All Items Used> Test End,States:OK\n");
932 
933 	printf("[Vendor Test]:<All Items Used> re init,States:OK\n");
934 	ret = vendor_storage_init();
935 	/* Read data */
936 	for (id = 0; id < item_num; id++) {
937 		memset(buffer, 0, size);
938 		ret = vendor_storage_read(id, buffer, size);
939 		if (ret < 0) {
940 			printf("[Vendor Test]:vendor read failed(id=%d)!\n", id);
941 			free(buffer);
942 			return ret;
943 		}
944 		/* check data Correctness */
945 		for (j = 0; j < size; j++) {
946 			if (*(buffer + j) != id) {
947 				printf("[Vendor Test]:Unexpected error occurs(id=%d)\n", id);
948 				printf("the data content is:\n");
949 				print_buffer(0, buffer, 1, size, 16);
950 
951 				free(buffer);
952 				return -1;
953 			}
954 		}
955 		debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size);
956 	}
957 	printf("[Vendor Test]:<All Items Used> Test End,States:OK\n");
958 #ifdef CONFIG_MTD_BLK
959 	if (bootdev_type == IF_TYPE_MTD)
960 		return 0;
961 #endif
962 	/*
963 	 * SECOND TEST: Overrides the maximum number of items to see if the
964 	 * return value matches the expectation
965 	 */
966 	printf("[Vendor Test]:<Overflow Items Cnt> Test Start...\n");
967 	/* Any id value that was not used before */
968 	id = item_num;
969 	printf("[Vendor Test]:id=%d, size=%d.\n", id, size);
970 	ret = vendor_storage_write(id, buffer, size);
971 	if (ret == -ENOMEM)
972 		printf("[Vendor Test]:<Overflow Items Cnt> Test End,States:OK\n");
973 	else
974 		printf("[Vendor Test]:<Overflow Items Cnt> Test End,States:Failed\n");
975 
976 	/* free buffer, remalloc later */
977 	free(buffer);
978 	buffer = NULL;
979 	/*
980 	 * remalloc memory and recalculate size to test memory overflow
981 	 * (1) item_num > 10: Memory is divided into 10 blocks,
982 	 * 11th memory will overflow.
983 	 * (2) 10 > item_num > 1: Memory is divided into item_num-1
984 	 * blocks. item_num block, memory will overflow.
985 	 * (3) item_num = 1: size = total_size + 512 Bytes, The first
986 	 * block, memory will overflow.
987 	 * The reason to do so is to minimize the size of the memory,
988 	 * making malloc easier to perform successfully.
989 	 */
990 	item_num = (item_num > 10) ? 10 : (item_num - 1);
991 	size = item_num ? (total_size / item_num) : (total_size + 512);
992 	size = (size + VENDOR_BTYE_ALIGN) & (~VENDOR_BTYE_ALIGN);
993 	/* Find item_num value that can make the memory overflow */
994 	for (id = 0; id <= item_num; id++) {
995 		if (((id + 1) * size) > total_size) {
996 			item_num = id;
997 			break;
998 		}
999 	}
1000 	/* malloc */
1001 	buffer = (u8 *)malloc(size);
1002 	if (buffer == NULL) {
1003 		printf("[Vendor Test]:Malloc failed(size=%d)!\n", size);
1004 		return -ENOMEM;
1005 	}
1006 
1007 	/* THIRD TEST: Single Item memory overflow test */
1008 	printf("[Vendor Test]:<Single Item Memory Overflow> Test Start...\n");
1009 	/* The value can be arbitrary */
1010 	memset(buffer, 'a', size);
1011 	/* Any id value that was used before */
1012 	id = 0;
1013 	printf("[Vendor Test]:id=%d, size=%d.\n", id, size);
1014 	ret = vendor_storage_write(id, buffer, size);
1015 	if (ret == size)
1016 		printf("[Vendor Test]:<Single Item Memory Overflow> Test End, States:OK\n");
1017 	else
1018 		printf("[Vendor Test]:<Single Item Memory Overflow> Test End, States:Failed\n");
1019 
1020 	/* FORTH TEST: Total memory overflow test */
1021 	printf("[Vendor Test]:<Total memory overflow> Test Start...\n");
1022 	printf("[Vendor Test]:item_num=%d, size=%d.\n", item_num, size);
1023 
1024 	vendor_test_reset();
1025 	for (id = 0; id < item_num; id++) {
1026 		memset(buffer, id, size);
1027 		ret = vendor_storage_write(id, buffer, size);
1028 		if (ret < 0) {
1029 			if ((id == item_num) && (ret == -ENOMEM)) {
1030 				printf("[Vendor Test]:<Total memory overflow> Test End, States:OK\n");
1031 				break;
1032 			} else {
1033 				printf("[Vendor Test]:<Total memory overflow> Test End, States:Failed\n");
1034 				break;
1035 			}
1036 		}
1037 		debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size);
1038 	}
1039 
1040 	/* Test end */
1041 	printf("[Vendor Test]:After Test, Vendor Resetting...\n");
1042 	vendor_test_reset();
1043 	printf("[Vendor Test]:Test End.\n");
1044 	free(buffer);
1045 
1046 	return 0;
1047 }
1048