[PATCH 13/30] ide: Refactor confusing loop code

Simon Glass sjg at chromium.org
Mon Mar 27 21:07:00 CEST 2023


This code is hard to follow as it uses #ifdef in a strange way. Adjust
it to avoid the preprocessor. Drop the special return for the non-ATAPI
case since we can rely on tries becoming 0 and exiting the loop.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 drivers/block/ide.c | 43 +++++++++++++++----------------------------
 1 file changed, 15 insertions(+), 28 deletions(-)

diff --git a/drivers/block/ide.c b/drivers/block/ide.c
index 782780fd302b..2f45bb6bffb2 100644
--- a/drivers/block/ide.c
+++ b/drivers/block/ide.c
@@ -555,10 +555,8 @@ static void ide_ident(struct blk_desc *dev_desc)
 {
 	unsigned char c;
 	hd_driveid_t iop;
-#ifdef CONFIG_ATAPI
 	bool is_atapi = false;
 	int tries = 1;
-#endif
 	int device;
 
 	device = dev_desc->devnum;
@@ -568,17 +566,16 @@ static void ide_ident(struct blk_desc *dev_desc)
 	 */
 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
 	dev_desc->uclass_id = UCLASS_IDE;
-#ifdef CONFIG_ATAPI
+	if (IS_ENABLED(CONFIG_ATAPI))
+		tries = 2;
 
-	tries = 2;
-
-	/* Warning: This will be tricky to read */
 	while (tries) {
 		/* check signature */
-		if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
-		    (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
-		    (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
-		    (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
+		if (IS_ENABLED(CONFIG_ATAPI) &&
+		    ide_inb(device, ATA_SECT_CNT) == 0x01 &&
+		    ide_inb(device, ATA_SECT_NUM) == 0x01 &&
+		    ide_inb(device, ATA_CYL_LOW) == 0x14 &&
+		    ide_inb(device, ATA_CYL_HIGH) == 0xeb) {
 			/* ATAPI Signature found */
 			is_atapi = true;
 			/*
@@ -590,9 +587,7 @@ static void ide_ident(struct blk_desc *dev_desc)
 			 * to become ready
 			 */
 			c = ide_wait(device, ATAPI_TIME_OUT);
-		} else
-#endif
-		{
+		} else {
 			/*
 			 * Start Ident Command
 			 */
@@ -606,8 +601,7 @@ static void ide_ident(struct blk_desc *dev_desc)
 
 		if (((c & ATA_STAT_DRQ) == 0) ||
 		    ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
-#ifdef CONFIG_ATAPI
-			{
+			if (IS_ENABLED(CONFIG_ATAPI)) {
 				/*
 				 * Need to soft reset the device
 				 * in case it's an ATAPI...
@@ -618,25 +612,18 @@ static void ide_ident(struct blk_desc *dev_desc)
 				mdelay(100);
 				ide_outb(device, ATA_COMMAND, 0x08);
 				mdelay(500);
+				/* Select device */
+				ide_outb(device, ATA_DEV_HD,
+					 ATA_LBA | ATA_DEVICE(device));
 			}
-			/*
-			 * Select device
-			 */
-			ide_outb(device, ATA_DEV_HD,
-				 ATA_LBA | ATA_DEVICE(device));
 			tries--;
-#else
-			return;
-#endif
-		}
-#ifdef CONFIG_ATAPI
-		else
+		} else {
 			break;
-	}			/* see above - ugly to read */
+		}
+	}
 
 	if (!tries)	/* Not found */
 		return;
-#endif
 
 	ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
 
-- 
2.40.0.348.gf938b09366-goog



More information about the U-Boot mailing list