[U-Boot] [PATCH 13/51] cfi_flash: Clean up code some more

Mario Six mario.six at gdsys.cc
Fri Jul 14 12:54:59 UTC 2017


Fix some more style violations (mostly mis-indented case statements),
and reduce the scope of some variables.

Signed-off-by: Mario Six <mario.six at gdsys.cc>
---

 drivers/mtd/cfi_flash.c | 610 ++++++++++++++++++++++++------------------------
 1 file changed, 305 insertions(+), 305 deletions(-)

diff --git a/drivers/mtd/cfi_flash.c b/drivers/mtd/cfi_flash.c
index 18be3bee53..2445632580 100644
--- a/drivers/mtd/cfi_flash.c
+++ b/drivers/mtd/cfi_flash.c
@@ -183,10 +183,10 @@ __maybe_weak u64 flash_read64(void *addr)
 flash_info_t *flash_get_info(ulong base)
 {
 	int i;
-	flash_info_t *info;
 
 	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
-		info = &flash_info[i];
+		flash_info_t *info = &flash_info[i];
+
 		if (info->size && info->start[0] <= base &&
 		    base <= info->start[0] + info->size - 1)
 			return info;
@@ -226,8 +226,6 @@ static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
 static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
 {
 	int i;
-	int cword_offset;
-	int cp_offset;
 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
 	u32 cmd_le = cpu_to_le32(cmd);
 #endif
@@ -235,7 +233,8 @@ static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
 	uchar *cp = (uchar *)cmdbuf;
 
 	for (i = info->portwidth; i > 0; i--) {
-		cword_offset = (info->portwidth - i) % info->chipwidth;
+		int cp_offset;
+		int cword_offset = (info->portwidth - i) % info->chipwidth;
 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
 		cp_offset = info->portwidth - i;
 		val = *((uchar *)&cmd_le + cword_offset);
@@ -1072,96 +1071,94 @@ int flash_erase(flash_info_t *info, int s_first, int s_last)
 	}
 
 	for (sect = s_first; sect <= s_last; sect++) {
+#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
+		int k;
+		int size;
+		int erased;
+		u32 *flash;
+#endif
+
 		if (ctrlc()) {
 			printf("\n");
 			return 1;
 		}
 
-		if (info->protect[sect] == 0) { /* not protected */
-#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
-			int k;
-			int size;
-			int erased;
-			u32 *flash;
+		if (info->protect[sect] != 0)
+			continue;
 
-			/*
-			 * Check if whole sector is erased
-			 */
-			size = flash_sector_size(info, sect);
-			erased = 1;
-			flash = (u32 *)info->start[sect];
-			/* divide by 4 for longword access */
-			size = size >> 2;
-			for (k = 0; k < size; k++) {
-				if (flash_read32(flash++) != 0xffffffff) {
-					erased = 0;
-					break;
-				}
-			}
-			if (erased) {
-				if (flash_verbose)
-					putc(',');
-				continue;
+#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
+		/*
+		 * Check if whole sector is erased
+		 */
+		size = flash_sector_size(info, sect);
+		erased = 1;
+		flash = (u32 *)info->start[sect];
+		/* divide by 4 for longword access */
+		size = size >> 2;
+		for (k = 0; k < size; k++) {
+			if (flash_read32(flash++) != 0xffffffff) {
+				erased = 0;
+				break;
 			}
+		}
+		if (erased) {
+			if (flash_verbose)
+				putc(',');
+			continue;
+		}
 #endif
-			switch (info->vendor) {
-			case CFI_CMDSET_INTEL_PROG_REGIONS:
-			case CFI_CMDSET_INTEL_STANDARD:
-			case CFI_CMDSET_INTEL_EXTENDED:
-				flash_write_cmd(info, sect, 0,
-						FLASH_CMD_CLEAR_STATUS);
-				flash_write_cmd(info, sect, 0,
-						FLASH_CMD_BLOCK_ERASE);
-				flash_write_cmd(info, sect, 0,
-						FLASH_CMD_ERASE_CONFIRM);
-				break;
-			case CFI_CMDSET_AMD_STANDARD:
-			case CFI_CMDSET_AMD_EXTENDED:
-				flash_unlock_seq(info, sect);
-				flash_write_cmd(info, sect,
-						info->addr_unlock1,
-						AMD_CMD_ERASE_START);
-				flash_unlock_seq(info, sect);
-				flash_write_cmd(info, sect, 0,
-						info->cmd_erase_sector);
-				break;
+		switch (info->vendor) {
+		case CFI_CMDSET_INTEL_PROG_REGIONS:
+		case CFI_CMDSET_INTEL_STANDARD:
+		case CFI_CMDSET_INTEL_EXTENDED:
+			flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
+			flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
+			flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
+			break;
+		case CFI_CMDSET_AMD_STANDARD:
+		case CFI_CMDSET_AMD_EXTENDED:
+			flash_unlock_seq(info, sect);
+			flash_write_cmd(info, sect, info->addr_unlock1,
+					AMD_CMD_ERASE_START);
+			flash_unlock_seq(info, sect);
+			flash_write_cmd(info, sect, 0, info->cmd_erase_sector);
+			break;
 #ifdef CONFIG_FLASH_CFI_LEGACY
-			case CFI_CMDSET_AMD_LEGACY:
-				flash_unlock_seq(info, 0);
-				flash_write_cmd(info, 0, info->addr_unlock1,
-						AMD_CMD_ERASE_START);
-				flash_unlock_seq(info, 0);
-				flash_write_cmd(info, sect, 0,
-						AMD_CMD_ERASE_SECTOR);
-				break;
+		case CFI_CMDSET_AMD_LEGACY:
+			flash_unlock_seq(info, 0);
+			flash_write_cmd(info, 0, info->addr_unlock1,
+					AMD_CMD_ERASE_START);
+			flash_unlock_seq(info, 0);
+			flash_write_cmd(info, sect, 0,
+					AMD_CMD_ERASE_SECTOR);
+			break;
 #endif
-			default:
-				debug("Unknown flash vendor %d\n",
-				      info->vendor);
-				break;
-			}
-
-			if (use_flash_status_poll(info)) {
-				cfiword_t cword;
-				void *dest;
+		default:
+			debug("Unknown flash vendor %d\n",
+			      info->vendor);
+			break;
+		}
 
-				cword.w64 = 0xffffffffffffffffULL;
-				dest = flash_map(info, sect, 0);
-				st = flash_status_poll(info, &cword, dest,
-						       info->erase_blk_tout,
-						       "erase");
-				flash_unmap(info, sect, 0, dest);
-			} else {
-				st = flash_full_status_check(info, sect,
-							     info->erase_blk_tout,
-							     "erase");
-			}
+		if (use_flash_status_poll(info)) {
+			cfiword_t cword;
+			void *dest;
 
-			if (st)
-				rcode = 1;
-			else if (flash_verbose)
-				putc('.');
+			cword.w64 = 0xffffffffffffffffULL;
+			dest = flash_map(info, sect, 0);
+			st = flash_status_poll(info, &cword, dest,
+					       info->erase_blk_tout,
+					       "erase");
+			flash_unmap(info, sect, 0, dest);
+		} else {
+			st = flash_full_status_check(info, sect,
+						     info->erase_blk_tout,
+						     "erase");
 		}
+
+		if (st)
+			rcode = 1;
+		else if (flash_verbose)
+			putc('.');
 	}
 
 	if (flash_verbose)
@@ -1335,7 +1332,8 @@ int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 	wp = (addr & ~(info->portwidth - 1));
 
 	/* handle unaligned start */
-	if ((aln = addr - wp) != 0) {
+	aln = addr - wp;
+	if (aln != 0) {
 		cword.w32 = 0;
 		p = (uchar *)wp;
 		for (i = 0; i < aln; ++i)
@@ -1366,7 +1364,8 @@ int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 			cword.w32 = 0;
 			for (i = 0; i < info->portwidth; i++)
 				flash_add_byte(info, &cword, *src++);
-			if ((rc = flash_write_cfiword(info, wp, cword)) != 0)
+			rc = flash_write_cfiword(info, wp, cword);
+			if (rc != 0)
 				return rc;
 			wp += info->portwidth;
 			cnt -= info->portwidth;
@@ -1377,7 +1376,8 @@ int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 		i = buffered_size - (wp % buffered_size);
 		if (i > cnt)
 			i = cnt;
-		if ((rc = flash_write_cfibuffer(info, wp, src, i)) != ERR_OK)
+		rc = flash_write_cfibuffer(info, wp, src, i);
+		if (rc != ERR_OK)
 			return rc;
 		i -= i & (info->portwidth - 1);
 		wp += i;
@@ -1393,7 +1393,8 @@ int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 		cword.w32 = 0;
 		for (i = 0; i < info->portwidth; i++)
 			flash_add_byte(info, &cword, *src++);
-		if ((rc = flash_write_cfiword(info, wp, cword)) != 0)
+		rc = flash_write_cfiword(info, wp, cword);
+		if (rc != 0)
 			return rc;
 		wp += info->portwidth;
 		cnt -= info->portwidth;
@@ -1567,9 +1568,9 @@ int flash_real_protect(flash_info_t *info, long sector, int prot)
 	 * flash_full_status_check() to work correctly
 	 */
 	flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
-	if ((retcode =
-	     flash_full_status_check(info, sector, info->erase_blk_tout,
-				     prot ? "protect" : "unprotect")) == 0) {
+	retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
+					  prot ? "protect" : "unprotect");
+	if (retcode == 0) {
 		info->protect[sector] = prot;
 
 		/*
@@ -1682,22 +1683,22 @@ static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
 
 static void cmdset_amd_read_jedec_ids(flash_info_t *info)
 {
-	ushort bankId = 0;
-	uchar manuId;
+	ushort bank_id = 0;
+	uchar manu_id;
 
 	flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
 	flash_unlock_seq(info, 0);
 	flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
 	udelay(1000); /* some flash are slow to respond */
 
-	manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
+	manu_id = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
 	/* JEDEC JEP106Z specifies ID codes up to bank 7 */
-	while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
-		bankId += 0x100;
-		manuId = flash_read_uchar(info,
-					  bankId | FLASH_OFFSET_MANUFACTURER_ID);
+	while (manu_id == FLASH_CONTINUATION_CODE && bank_id < 0x800) {
+		bank_id += 0x100;
+		manu_id = flash_read_uchar(info,
+					   bank_id | FLASH_OFFSET_MANUFACTURER_ID);
 	}
-	info->manufacturer_id = manuId;
+	info->manufacturer_id = manu_id;
 
 	switch (info->chipwidth) {
 	case FLASH_CFI_8BIT:
@@ -1870,7 +1871,7 @@ static void __flash_cmd_reset(flash_info_t *info)
 }
 
 void flash_cmd_reset(flash_info_t *info)
-	__attribute__((weak,alias("__flash_cmd_reset")));
+	__attribute__((weak, alias("__flash_cmd_reset")));
 
 static int __flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
 {
@@ -2052,8 +2053,6 @@ ulong flash_get_size(phys_addr_t base, int banknum)
 	unsigned long tmp;
 	int size_ratio;
 	uchar num_erase_regions;
-	int erase_region_size;
-	int erase_region_count;
 	struct cfi_qry qry;
 	unsigned long max_size;
 
@@ -2067,196 +2066,200 @@ ulong flash_get_size(phys_addr_t base, int banknum)
 
 	info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
 
-	if (flash_detect_cfi(info, &qry)) {
-		info->vendor = le16_to_cpu(get_unaligned(&qry.p_id));
-		info->ext_addr = le16_to_cpu(get_unaligned(&qry.p_adr));
-		num_erase_regions = qry.num_erase_regions;
+	if (!flash_detect_cfi(info, &qry))
+		return info->size;
 
-		if (info->ext_addr) {
-			info->cfi_version = (ushort)flash_read_uchar (info,
-						info->ext_addr + 3) << 8;
-			info->cfi_version |= (ushort)flash_read_uchar (info,
-						info->ext_addr + 4);
-		}
+	info->vendor = le16_to_cpu(get_unaligned(&qry.p_id));
+	info->ext_addr = le16_to_cpu(get_unaligned(&qry.p_adr));
+	num_erase_regions = qry.num_erase_regions;
+
+	if (info->ext_addr) {
+		info->cfi_version = (ushort)flash_read_uchar (info,
+					info->ext_addr + 3) << 8;
+		info->cfi_version |= (ushort)flash_read_uchar (info,
+					info->ext_addr + 4);
+	}
 
 #ifdef DEBUG
-		flash_printqry(&qry);
+	flash_printqry(&qry);
 #endif
 
-		switch (info->vendor) {
-		case CFI_CMDSET_INTEL_PROG_REGIONS:
-		case CFI_CMDSET_INTEL_STANDARD:
-		case CFI_CMDSET_INTEL_EXTENDED:
-			cmdset_intel_init(info, &qry);
-			break;
-		case CFI_CMDSET_AMD_STANDARD:
-		case CFI_CMDSET_AMD_EXTENDED:
-			cmdset_amd_init(info, &qry);
-			break;
-		default:
-			printf("CFI: Unknown command set 0x%x\n",
-			       info->vendor);
-			/*
-			 * Unfortunately, this means we don't know how
-			 * to get the chip back to Read mode. Might
-			 * as well try an Intel-style reset...
-			 */
-			flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
-			return 0;
-		}
+	switch (info->vendor) {
+	case CFI_CMDSET_INTEL_PROG_REGIONS:
+	case CFI_CMDSET_INTEL_STANDARD:
+	case CFI_CMDSET_INTEL_EXTENDED:
+		cmdset_intel_init(info, &qry);
+		break;
+	case CFI_CMDSET_AMD_STANDARD:
+	case CFI_CMDSET_AMD_EXTENDED:
+		cmdset_amd_init(info, &qry);
+		break;
+	default:
+		printf("CFI: Unknown command set 0x%x\n",
+		       info->vendor);
+		/*
+		 * Unfortunately, this means we don't know how
+		 * to get the chip back to Read mode. Might
+		 * as well try an Intel-style reset...
+		 */
+		flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
+		return 0;
+	}
 
-		/* Do manufacturer-specific fixups */
-		switch (info->manufacturer_id) {
-		case 0x0001: /* AMD */
-		case 0x0037: /* AMIC */
-			flash_fixup_amd(info, &qry);
-			break;
-		case 0x001f:
-			flash_fixup_atmel(info, &qry);
-			break;
-		case 0x0020:
-			flash_fixup_stm(info, &qry);
-			break;
-		case 0x00bf: /* SST */
-			flash_fixup_sst(info, &qry);
-			break;
-		case 0x0089: /* Numonyx */
-			flash_fixup_num(info, &qry);
+	/* Do manufacturer-specific fixups */
+	switch (info->manufacturer_id) {
+	case 0x0001: /* AMD */
+	case 0x0037: /* AMIC */
+		flash_fixup_amd(info, &qry);
+		break;
+	case 0x001f:
+		flash_fixup_atmel(info, &qry);
+		break;
+	case 0x0020:
+		flash_fixup_stm(info, &qry);
+		break;
+	case 0x00bf: /* SST */
+		flash_fixup_sst(info, &qry);
+		break;
+	case 0x0089: /* Numonyx */
+		flash_fixup_num(info, &qry);
+		break;
+	}
+
+	debug("manufacturer is %d\n", info->vendor);
+	debug("manufacturer id is 0x%x\n", info->manufacturer_id);
+	debug("device id is 0x%x\n", info->device_id);
+	debug("device id2 is 0x%x\n", info->device_id2);
+	debug("cfi version is 0x%04x\n", info->cfi_version);
+
+	size_ratio = info->portwidth / info->chipwidth;
+	/* if the chip is x8/x16 reduce the ratio by half */
+	if ((info->interface == FLASH_CFI_X8X16) &&
+	    (info->chipwidth == FLASH_CFI_BY8)) {
+		size_ratio >>= 1;
+	}
+	debug("size_ratio %d port %d bits chip %d bits\n",
+	      size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
+	      info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
+	info->size = 1 << qry.dev_size;
+	/* multiply the size by the number of chips */
+	info->size *= size_ratio;
+	max_size = cfi_flash_bank_size(banknum);
+	if (max_size && (info->size > max_size)) {
+		debug("[truncated from %ldMiB]", info->size >> 20);
+		info->size = max_size;
+	}
+	debug("found %d erase regions\n", num_erase_regions);
+	sect_cnt = 0;
+	sector = base;
+	for (i = 0; i < num_erase_regions; i++) {
+		int erase_region_size;
+		int erase_region_count;
+
+		if (i > NUM_ERASE_REGIONS) {
+			printf("%d erase regions found, only %d used\n",
+			       num_erase_regions, NUM_ERASE_REGIONS);
 			break;
 		}
 
-		debug("manufacturer is %d\n", info->vendor);
-		debug("manufacturer id is 0x%x\n", info->manufacturer_id);
-		debug("device id is 0x%x\n", info->device_id);
-		debug("device id2 is 0x%x\n", info->device_id2);
-		debug("cfi version is 0x%04x\n", info->cfi_version);
-
-		size_ratio = info->portwidth / info->chipwidth;
-		/* if the chip is x8/x16 reduce the ratio by half */
-		if ((info->interface == FLASH_CFI_X8X16) &&
-		    (info->chipwidth == FLASH_CFI_BY8)) {
-			size_ratio >>= 1;
-		}
-		debug("size_ratio %d port %d bits chip %d bits\n",
-		      size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
-		      info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
-		info->size = 1 << qry.dev_size;
-		/* multiply the size by the number of chips */
-		info->size *= size_ratio;
-		max_size = cfi_flash_bank_size(banknum);
-		if (max_size && (info->size > max_size)) {
-			debug("[truncated from %ldMiB]", info->size >> 20);
-			info->size = max_size;
-		}
-		debug("found %d erase regions\n", num_erase_regions);
-		sect_cnt = 0;
-		sector = base;
-		for (i = 0; i < num_erase_regions; i++) {
-			if (i > NUM_ERASE_REGIONS) {
-				printf("%d erase regions found, only %d used\n",
-				       num_erase_regions, NUM_ERASE_REGIONS);
+		tmp = le32_to_cpu(get_unaligned(
+					&qry.erase_region_info[i]));
+		debug("erase region %u: 0x%08lx\n", i, tmp);
+
+		erase_region_count = (tmp & 0xffff) + 1;
+		tmp >>= 16;
+		erase_region_size =
+			(tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
+		debug("erase_region_count = %d erase_region_size = %d\n",
+		      erase_region_count, erase_region_size);
+		for (j = 0; j < erase_region_count; j++) {
+			if (sector - base >= info->size)
+				break;
+			if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
+				printf("ERROR: too many flash sectors\n");
 				break;
 			}
+			info->start[sect_cnt] =
+				(ulong)map_physmem(sector,
+						   info->portwidth,
+						   MAP_NOCACHE);
+			sector += (erase_region_size * size_ratio);
 
-			tmp = le32_to_cpu(get_unaligned(
-						&qry.erase_region_info[i]));
-			debug("erase region %u: 0x%08lx\n", i, tmp);
-
-			erase_region_count = (tmp & 0xffff) + 1;
-			tmp >>= 16;
-			erase_region_size =
-				(tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
-			debug("erase_region_count = %d erase_region_size = %d\n",
-			      erase_region_count, erase_region_size);
-			for (j = 0; j < erase_region_count; j++) {
-				if (sector - base >= info->size)
-					break;
-				if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
-					printf("ERROR: too many flash sectors\n");
-					break;
-				}
-				info->start[sect_cnt] =
-					(ulong)map_physmem(sector,
-							   info->portwidth,
-							   MAP_NOCACHE);
-				sector += (erase_region_size * size_ratio);
-
+			/*
+			 * Only read protection status from
+			 * supported devices (intel...)
+			 */
+			switch (info->vendor) {
+			case CFI_CMDSET_INTEL_PROG_REGIONS:
+			case CFI_CMDSET_INTEL_EXTENDED:
+			case CFI_CMDSET_INTEL_STANDARD:
 				/*
-				 * Only read protection status from
-				 * supported devices (intel...)
+				 * Set flash to read-id mode. Otherwise
+				 * reading protected status is not
+				 * guaranteed.
 				 */
-				switch (info->vendor) {
-				case CFI_CMDSET_INTEL_PROG_REGIONS:
-				case CFI_CMDSET_INTEL_EXTENDED:
-				case CFI_CMDSET_INTEL_STANDARD:
-					/*
-					 * Set flash to read-id mode. Otherwise
-					 * reading protected status is not
-					 * guaranteed.
-					 */
-					flash_write_cmd(info, sect_cnt, 0,
-							FLASH_CMD_READ_ID);
-					info->protect[sect_cnt] =
-						flash_isset(info, sect_cnt,
-							    FLASH_OFFSET_PROTECT,
-							    FLASH_STATUS_PROTECT);
-					flash_write_cmd(info, sect_cnt, 0,
-							FLASH_CMD_RESET);
-					break;
-				case CFI_CMDSET_AMD_EXTENDED:
-				case CFI_CMDSET_AMD_STANDARD:
-					if (!info->legacy_unlock) {
-						/* default: not protected */
-						info->protect[sect_cnt] = 0;
-						break;
-					}
-
-					/* Read protection (PPB) from sector */
-					flash_write_cmd(info, 0, 0,
-							info->cmd_reset);
-					flash_unlock_seq(info, 0);
-					flash_write_cmd(info, 0,
-							info->addr_unlock1,
-							FLASH_CMD_READ_ID);
-					info->protect[sect_cnt] =
-						flash_isset(
-							info, sect_cnt,
-							FLASH_OFFSET_PROTECT,
-							FLASH_STATUS_PROTECT);
-					break;
-				default:
+				flash_write_cmd(info, sect_cnt, 0,
+						FLASH_CMD_READ_ID);
+				info->protect[sect_cnt] =
+					flash_isset(info, sect_cnt,
+						    FLASH_OFFSET_PROTECT,
+						    FLASH_STATUS_PROTECT);
+				flash_write_cmd(info, sect_cnt, 0,
+						FLASH_CMD_RESET);
+				break;
+			case CFI_CMDSET_AMD_EXTENDED:
+			case CFI_CMDSET_AMD_STANDARD:
+				if (!info->legacy_unlock) {
 					/* default: not protected */
 					info->protect[sect_cnt] = 0;
+					break;
 				}
 
-				sect_cnt++;
+				/* Read protection (PPB) from sector */
+				flash_write_cmd(info, 0, 0,
+						info->cmd_reset);
+				flash_unlock_seq(info, 0);
+				flash_write_cmd(info, 0,
+						info->addr_unlock1,
+						FLASH_CMD_READ_ID);
+				info->protect[sect_cnt] =
+					flash_isset(
+						info, sect_cnt,
+						FLASH_OFFSET_PROTECT,
+						FLASH_STATUS_PROTECT);
+				break;
+			default:
+				/* default: not protected */
+				info->protect[sect_cnt] = 0;
 			}
-		}
 
-		info->sector_count = sect_cnt;
-		info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
-		tmp = 1 << qry.block_erase_timeout_typ;
-		info->erase_blk_tout = tmp *
-			(1 << qry.block_erase_timeout_max);
-		tmp = (1 << qry.buf_write_timeout_typ) *
-			(1 << qry.buf_write_timeout_max);
-
-		/* round up when converting to ms */
-		info->buffer_write_tout = (tmp + 999) / 1000;
-		tmp = (1 << qry.word_write_timeout_typ) *
-			(1 << qry.word_write_timeout_max);
-		/* round up when converting to ms */
-		info->write_tout = (tmp + 999) / 1000;
-		info->flash_id = FLASH_MAN_CFI;
-		if ((info->interface == FLASH_CFI_X8X16) &&
-		    (info->chipwidth == FLASH_CFI_BY8)) {
-			/* XXX - Need to test on x8/x16 in parallel. */
-			info->portwidth >>= 1;
+			sect_cnt++;
 		}
+	}
 
-		flash_write_cmd(info, 0, 0, info->cmd_reset);
+	info->sector_count = sect_cnt;
+	info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
+	tmp = 1 << qry.block_erase_timeout_typ;
+	info->erase_blk_tout = tmp *
+		(1 << qry.block_erase_timeout_max);
+	tmp = (1 << qry.buf_write_timeout_typ) *
+		(1 << qry.buf_write_timeout_max);
+
+	/* round up when converting to ms */
+	info->buffer_write_tout = (tmp + 999) / 1000;
+	tmp = (1 << qry.word_write_timeout_typ) *
+		(1 << qry.word_write_timeout_max);
+	/* round up when converting to ms */
+	info->write_tout = (tmp + 999) / 1000;
+	info->flash_id = FLASH_MAN_CFI;
+	if ((info->interface == FLASH_CFI_X8X16) &&
+	    (info->chipwidth == FLASH_CFI_BY8)) {
+		/* XXX - Need to test on x8/x16 in parallel. */
+		info->portwidth >>= 1;
 	}
 
+	flash_write_cmd(info, 0, 0, info->cmd_reset);
+
 	return info->size;
 }
 
@@ -2375,54 +2378,51 @@ unsigned long flash_init(void)
 			       i + 1, flash_info[i].size,
 			       flash_info[i].size >> 20);
 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
+			continue;
 		}
 #ifdef CONFIG_SYS_FLASH_PROTECTION
-		else if (strcmp(s, "yes") == 0) {
+		if (strcmp(s, "yes") != 0)
+			continue;
+
+		/*
+		 * Only the U-Boot image and it's environment is protected, all
+		 * other sectors are unprotected (unlocked) if flash hardware
+		 * protection is used (CONFIG_SYS_FLASH_PROTECTION) and the
+		 * environment variable "unlock" is set to "yes".
+		 */
+		if (flash_info[i].legacy_unlock) {
+			int k;
+
 			/*
-			 * Only the U-Boot image and it's environment
-			 * is protected, all other sectors are
-			 * unprotected (unlocked) if flash hardware
-			 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
-			 * and the environment variable "unlock" is
-			 * set to "yes".
+			 * Disable legacy_unlock temporarily, since
+			 * flash_real_protect would relock all other sectors
+			 * again otherwise.
 			 */
-			if (flash_info[i].legacy_unlock) {
-				int k;
+			flash_info[i].legacy_unlock = 0;
 
-				/*
-				 * Disable legacy_unlock temporarily,
-				 * since flash_real_protect would
-				 * relock all other sectors again
-				 * otherwise.
-				 */
-				flash_info[i].legacy_unlock = 0;
+			/*
+			 * Legacy unlocking (e.g. Intel J3) -> unlock only one
+			 * sector. This will unlock all sectors.
+			 */
+			flash_real_protect(&flash_info[i], 0, 0);
 
-				/*
-				 * Legacy unlocking (e.g. Intel J3) ->
-				 * unlock only one sector. This will
-				 * unlock all sectors.
-				 */
-				flash_real_protect(&flash_info[i], 0, 0);
+			flash_info[i].legacy_unlock = 1;
 
-				flash_info[i].legacy_unlock = 1;
+			/*
+			 * Manually mark other sectors as unlocked (unprotected)
+			 */
+			for (k = 1; k < flash_info[i].sector_count; k++)
+				flash_info[i].protect[k] = 0;
 
-				/*
-				 * Manually mark other sectors as
-				 * unlocked (unprotected)
-				 */
-				for (k = 1; k < flash_info[i].sector_count; k++)
-					flash_info[i].protect[k] = 0;
-			} else {
-				/*
-				 * No legancy unlocking -> unlock all sectors
-				 */
-				flash_protect(FLAG_PROTECT_CLEAR,
-					      flash_info[i].start[0],
-					      flash_info[i].start[0]
-					      + flash_info[i].size - 1,
-					      &flash_info[i]);
-			}
+			continue;
 		}
+
+		/*
+		 * No legacy unlocking -> unlock all sectors
+		 */
+		flash_protect(FLAG_PROTECT_CLEAR, flash_info[i].start[0],
+			      flash_info[i].start[0] + flash_info[i].size - 1,
+			      &flash_info[i]);
 #endif /* CONFIG_SYS_FLASH_PROTECTION */
 	}
 
-- 
2.11.0



More information about the U-Boot mailing list