return 0;
 }
 
+static int spinand_init_flash(struct spinand_device *spinand)
+{
+       struct device *dev = &spinand->spimem->spi->dev;
+       struct nand_device *nand = spinand_to_nand(spinand);
+       int ret, i;
+
+       ret = spinand_read_cfg(spinand);
+       if (ret)
+               return ret;
+
+       ret = spinand_init_quad_enable(spinand);
+       if (ret)
+               return ret;
+
+       ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
+       if (ret)
+               return ret;
+
+       ret = spinand_manufacturer_init(spinand);
+       if (ret) {
+               dev_err(dev,
+               "Failed to initialize the SPI NAND chip (err = %d)\n",
+               ret);
+               return ret;
+       }
+
+       /* After power up, all blocks are locked, so unlock them here. */
+       for (i = 0; i < nand->memorg.ntargets; i++) {
+               ret = spinand_select_target(spinand, i);
+               if (ret)
+                       break;
+
+               ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
+               if (ret)
+                       break;
+       }
+
+       if (ret)
+               spinand_manufacturer_cleanup(spinand);
+
+       return ret;
+}
+
 static int spinand_init(struct spinand_device *spinand)
 {
        struct device *dev = &spinand->spimem->spi->dev;
        struct mtd_info *mtd = spinand_to_mtd(spinand);
        struct nand_device *nand = mtd_to_nanddev(mtd);
-       int ret, i;
+       int ret;
 
        /*
         * We need a scratch buffer because the spi_mem interface requires that
        if (ret)
                goto err_free_bufs;
 
-       ret = spinand_read_cfg(spinand);
-       if (ret)
-               goto err_free_bufs;
-
-       ret = spinand_init_quad_enable(spinand);
-       if (ret)
-               goto err_free_bufs;
-
-       ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
+       ret = spinand_init_flash(spinand);
        if (ret)
                goto err_free_bufs;
 
-       ret = spinand_manufacturer_init(spinand);
-       if (ret) {
-               dev_err(dev,
-                       "Failed to initialize the SPI NAND chip (err = %d)\n",
-                       ret);
-               goto err_free_bufs;
-       }
-
        ret = spinand_create_dirmaps(spinand);
        if (ret) {
                dev_err(dev,
                goto err_manuf_cleanup;
        }
 
-       /* After power up, all blocks are locked, so unlock them here. */
-       for (i = 0; i < nand->memorg.ntargets; i++) {
-               ret = spinand_select_target(spinand, i);
-               if (ret)
-                       goto err_manuf_cleanup;
-
-               ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
-               if (ret)
-                       goto err_manuf_cleanup;
-       }
-
        ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
        if (ret)
                goto err_manuf_cleanup;