mmc: store hwpart in the block device

This will allow us to have multiple block device structs each referring
to the same eMMC device, yet different HW partitions.

For now, there is still a single block device per eMMC device. As before,
this block device always accesses whichever HW partition was most recently
selected. Clients wishing to make use of multiple block devices referring
to different HW partitions can simply take a copy of this block device
once it points at the correct HW partition, and use each one as they wish.
This feature will be used by the next patch.

In the future, perhaps get_device() could be enhanced to return a
dynamically allocated block device struct, to avoid the client needing to
copy it in order to maintain multiple block devices. However, this would
require all users to be updated to free those block device structs at some
point, which is rather a large change.

Most callers of mmc_switch_part() wish to permanently switch the default
MMC block device's HW partition. Enhance mmc_switch_part() so that it does
this. This removes the need for callers to do this. However,
common/env_mmc.c needs to save and restore the current HW partition. Make
it do this more explicitly.

Replace use of mmc_switch_part() with mmc_select_hwpart() in order to
remove duplicate code that skips the call if that HW partition is already
selected.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
diff --git a/drivers/dfu/dfu_mmc.c b/drivers/dfu/dfu_mmc.c
index 7ff2a81..395d472 100644
--- a/drivers/dfu/dfu_mmc.c
+++ b/drivers/dfu/dfu_mmc.c
@@ -20,23 +20,6 @@
 static long dfu_file_buf_len;
 static long dfu_file_buf_filled;
 
-static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part)
-{
-	int ret;
-
-	if (part == mmc->part_num)
-		return 0;
-
-	ret = mmc_switch_part(dfu->data.mmc.dev_num, part);
-	if (ret) {
-		error("Cannot switch to partition %d\n", part);
-		return ret;
-	}
-	mmc->part_num = part;
-
-	return 0;
-}
-
 static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
 			u64 offset, void *buf, long *len)
 {
@@ -66,8 +49,9 @@
 	}
 
 	if (dfu->data.mmc.hw_partition >= 0) {
-		part_num_bkp = mmc->part_num;
-		ret = mmc_access_part(dfu, mmc, dfu->data.mmc.hw_partition);
+		part_num_bkp = mmc->block_dev.hwpart;
+		ret = mmc_select_hwpart(dfu->data.mmc.dev_num,
+					dfu->data.mmc.hw_partition);
 		if (ret)
 			return ret;
 	}
@@ -91,12 +75,12 @@
 	if (n != blk_count) {
 		error("MMC operation failed");
 		if (dfu->data.mmc.hw_partition >= 0)
-			mmc_access_part(dfu, mmc, part_num_bkp);
+			mmc_select_hwpart(dfu->data.mmc.dev_num, part_num_bkp);
 		return -EIO;
 	}
 
 	if (dfu->data.mmc.hw_partition >= 0) {
-		ret = mmc_access_part(dfu, mmc, part_num_bkp);
+		ret = mmc_select_hwpart(dfu->data.mmc.dev_num, part_num_bkp);
 		if (ret)
 			return ret;
 	}