diff mbox

[U-Boot,05/12] cmd_sf: Define a functions for parsing read and write instructions

Message ID 1356952428-19824-6-git-send-email-jagannadh.teki@gmail.com
State Superseded
Delegated to: Jagannadha Sutradharudu Teki
Headers show

Commit Message

Jagan Teki Dec. 31, 2012, 11:13 a.m. UTC
This patch provides to define a separate functions for parsing read
and write instructions by taking instruction argument from user.

So-that the common functions can used in a different levels for
parsing read and write instructions.

Signed-off-by: Jagannadha Sutradharudu Teki <jagannadh.teki@gmail.com>
---
 common/cmd_sf.c |   70 ++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 54 insertions(+), 16 deletions(-)

Comments

Simon Glass Jan. 11, 2013, 2:18 a.m. UTC | #1
Hi Jagannadha,

On Mon, Dec 31, 2012 at 3:13 AM, Jagannadha Sutradharudu Teki
<jagannadh.teki@gmail.com> wrote:
> This patch provides to define a separate functions for parsing read
> and write instructions by taking instruction argument from user.
>
> So-that the common functions can used in a different levels for
> parsing read and write instructions.
>
> Signed-off-by: Jagannadha Sutradharudu Teki <jagannadh.teki@gmail.com>
> ---
>  common/cmd_sf.c |   70 ++++++++++++++++++++++++++++++++++++++++++------------
>  1 files changed, 54 insertions(+), 16 deletions(-)
>
> diff --git a/common/cmd_sf.c b/common/cmd_sf.c
> index 4cfd48a..d59ecce 100644
> --- a/common/cmd_sf.c
> +++ b/common/cmd_sf.c
> @@ -234,6 +234,48 @@ static int spi_flash_update(struct spi_flash *flash, u8 wr_inst, u8 rd_inst,
>         return 0;
>  }
>
> +/*
> + * This function parsed the write instruction for write operation
> + *
> + * Input:
> + *    arg: specified write instruction from user
> + * Output:
> + *    wr_inst: parsed write instruction for write operation
> + * Return:
> + *    1: for Unknown wr_inst from user
> + *    0: Success
> + */
> +static int sf_parse_wr_inst_arg(char *arg, u8 *wr_inst)
> +{
> +       if (strcmp(arg, "pp") == 0)
> +               *wr_inst = CMD_PAGE_PROGRAM;
> +       else
> +               return 1;
> +
> +       return 0;
> +}
> +
> +/*
> + * This function parsed the read instruction for read operation
> + *
> + * Input:
> + *    arg: specified read instruction from user
> + * Output:
> + *    rd_inst: parsed read instruction for write operation
> + * Return:
> + *    1: for Unknown rd_inst from user
> + *    0: Success
> + */
> +static int sf_parse_rd_inst_arg(char *arg, u8 *rd_inst)
> +{
> +       if (strcmp(arg, "afr") == 0)
> +               *rd_inst = CMD_READ_ARRAY_FAST;
> +       else
> +               return 1;
> +
> +       return 0;
> +}
> +
>  static int do_spi_flash_read_write(int argc, char * const argv[])
>  {
>         unsigned long addr;
> @@ -281,41 +323,37 @@ static int do_spi_flash_read_write(int argc, char * const argv[])
>         }
>
>         if (strcmp(argv[0], "update") == 0) {
> -               if (strcmp(argv[1], "pp") == 0)
> -                       wr_inst = CMD_PAGE_PROGRAM;
> -               else {
> +               ret = sf_parse_wr_inst_arg(argv[1], &wr_inst);
> +               if (ret) {
>                         printf("SF: Unknown %s wr_inst on 'sf update'\n",
>                                         argv[1]);
> -                       return 1;
> +                       return ret;
>                 }
>
> -               if (strcmp(argv[2], "afr") == 0)
> -                       rd_inst = CMD_READ_ARRAY_FAST;
> -               else {

Are you removing code you added in a previous patch? Why not just
define sf_parse_rd_inst_arg() in the first patch and use it
everywhere?

Also do you need to parse differently for read and write? Would be
nice to avoid that.

> +               ret = sf_parse_rd_inst_arg(argv[2], &rd_inst);
> +               if (ret) {
>                         printf("SF: Unknown %s rd_inst on 'sf update'\n",
>                                         argv[2]);
> -                       return 1;
> +                       return ret;
>                 }
>
>                 ret = spi_flash_update(flash, wr_inst, rd_inst,
>                                         offset, len, buf);
>         } else if (strcmp(argv[0], "read") == 0) {
> -               if (strcmp(argv[1], "afr") == 0)
> -                       rd_inst = CMD_READ_ARRAY_FAST;
> -               else {
> +               ret = sf_parse_rd_inst_arg(argv[1], &rd_inst);
> +               if (ret) {
>                         printf("SF: Unknown %s rd_inst on 'sf read'\n",
>                                         argv[1]);
> -                       return 1;
> +                       return ret;
>                 }
>
>                 ret = spi_flash_read(flash, rd_inst, offset, len, buf);
>         } else {
> -               if (strcmp(argv[1], "pp") == 0)
> -                       wr_inst = CMD_PAGE_PROGRAM;
> -               else {
> +               ret = sf_parse_wr_inst_arg(argv[1], &wr_inst);
> +               if (ret) {
>                         printf("SF: Unknown %s wr_inst on 'sf write'\n",
>                                         argv[1]);
> -                       return 1;
> +                       return ret;
>                 }
>
>                 ret = spi_flash_write(flash, wr_inst, offset, len, buf);
> --
> 1.7.0.4
>
> _______________________________________________
> U-Boot mailing list
> U-Boot@lists.denx.de
> http://lists.denx.de/mailman/listinfo/u-boot

Regards,
Simon
diff mbox

Patch

diff --git a/common/cmd_sf.c b/common/cmd_sf.c
index 4cfd48a..d59ecce 100644
--- a/common/cmd_sf.c
+++ b/common/cmd_sf.c
@@ -234,6 +234,48 @@  static int spi_flash_update(struct spi_flash *flash, u8 wr_inst, u8 rd_inst,
 	return 0;
 }
 
+/*
+ * This function parsed the write instruction for write operation
+ *
+ * Input:
+ *    arg: specified write instruction from user
+ * Output:
+ *    wr_inst: parsed write instruction for write operation
+ * Return:
+ *    1: for Unknown wr_inst from user
+ *    0: Success
+ */
+static int sf_parse_wr_inst_arg(char *arg, u8 *wr_inst)
+{
+	if (strcmp(arg, "pp") == 0)
+		*wr_inst = CMD_PAGE_PROGRAM;
+	else
+		return 1;
+
+	return 0;
+}
+
+/*
+ * This function parsed the read instruction for read operation
+ *
+ * Input:
+ *    arg: specified read instruction from user
+ * Output:
+ *    rd_inst: parsed read instruction for write operation
+ * Return:
+ *    1: for Unknown rd_inst from user
+ *    0: Success
+ */
+static int sf_parse_rd_inst_arg(char *arg, u8 *rd_inst)
+{
+	if (strcmp(arg, "afr") == 0)
+		*rd_inst = CMD_READ_ARRAY_FAST;
+	else
+		return 1;
+
+	return 0;
+}
+
 static int do_spi_flash_read_write(int argc, char * const argv[])
 {
 	unsigned long addr;
@@ -281,41 +323,37 @@  static int do_spi_flash_read_write(int argc, char * const argv[])
 	}
 
 	if (strcmp(argv[0], "update") == 0) {
-		if (strcmp(argv[1], "pp") == 0)
-			wr_inst = CMD_PAGE_PROGRAM;
-		else {
+		ret = sf_parse_wr_inst_arg(argv[1], &wr_inst);
+		if (ret) {
 			printf("SF: Unknown %s wr_inst on 'sf update'\n",
 					argv[1]);
-			return 1;
+			return ret;
 		}
 
-		if (strcmp(argv[2], "afr") == 0)
-			rd_inst = CMD_READ_ARRAY_FAST;
-		else {
+		ret = sf_parse_rd_inst_arg(argv[2], &rd_inst);
+		if (ret) {
 			printf("SF: Unknown %s rd_inst on 'sf update'\n",
 					argv[2]);
-			return 1;
+			return ret;
 		}
 
 		ret = spi_flash_update(flash, wr_inst, rd_inst,
 					offset, len, buf);
 	} else if (strcmp(argv[0], "read") == 0) {
-		if (strcmp(argv[1], "afr") == 0)
-			rd_inst = CMD_READ_ARRAY_FAST;
-		else {
+		ret = sf_parse_rd_inst_arg(argv[1], &rd_inst);
+		if (ret) {
 			printf("SF: Unknown %s rd_inst on 'sf read'\n",
 					argv[1]);
-			return 1;
+			return ret;
 		}
 
 		ret = spi_flash_read(flash, rd_inst, offset, len, buf);
 	} else {
-		if (strcmp(argv[1], "pp") == 0)
-			wr_inst = CMD_PAGE_PROGRAM;
-		else {
+		ret = sf_parse_wr_inst_arg(argv[1], &wr_inst);
+		if (ret) {
 			printf("SF: Unknown %s wr_inst on 'sf write'\n",
 					argv[1]);
-			return 1;
+			return ret;
 		}
 
 		ret = spi_flash_write(flash, wr_inst, offset, len, buf);