sf: Remove non-meaningful comments
[oweals/u-boot.git] / drivers / mtd / spi / spi_flash.c
index fa0e79966cf4253fde639c1f70556b57779d8277..813f08dca451e4f29d24a3f438a5a94dfec33f7b 100644 (file)
@@ -165,7 +165,8 @@ bar_end:
        return flash->bank_curr;
 }
 
-static int spi_flash_read_bar(struct spi_flash *flash, u8 idcode0)
+static int spi_flash_read_bar(struct spi_flash *flash,
+                             const struct spi_flash_info *info)
 {
        u8 curr_bank = 0;
        int ret;
@@ -173,7 +174,7 @@ static int spi_flash_read_bar(struct spi_flash *flash, u8 idcode0)
        if (flash->size <= SPI_FLASH_16MB_BOUN)
                goto bar_end;
 
-       switch (idcode0) {
+       switch (JEDEC_MFR(info)) {
        case SPI_FLASH_CFI_MFR_SPANSION:
                flash->bank_read_cmd = CMD_BANKADDR_BRRD;
                flash->bank_write_cmd = CMD_BANKADDR_BRWR;
@@ -924,9 +925,35 @@ static int micron_quad_enable(struct spi_flash *flash)
 }
 #endif
 
-static int set_quad_mode(struct spi_flash *flash, u8 idcode0)
+static const struct spi_flash_info *spi_flash_read_id(struct spi_flash *flash)
 {
-       switch (idcode0) {
+       int                             tmp;
+       u8                              id[SPI_FLASH_MAX_ID_LEN];
+       const struct spi_flash_info     *info;
+
+       tmp = spi_flash_cmd(flash->spi, CMD_READ_ID, id, SPI_FLASH_MAX_ID_LEN);
+       if (tmp < 0) {
+               printf("SF: error %d reading JEDEC ID\n", tmp);
+               return ERR_PTR(tmp);
+       }
+
+       info = spi_flash_ids;
+       for (; info->name != NULL; info++) {
+               if (info->id_len) {
+                       if (!memcmp(info->id, id, info->id_len))
+                               return info;
+               }
+       }
+
+       printf("SF: unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
+              id[0], id[1], id[2]);
+       return ERR_PTR(-ENODEV);
+}
+
+static int set_quad_mode(struct spi_flash *flash,
+                        const struct spi_flash_info *info)
+{
+       switch (JEDEC_MFR(info)) {
 #ifdef CONFIG_SPI_FLASH_MACRONIX
        case SPI_FLASH_CFI_MFR_MACRONIX:
                return macronix_quad_enable(flash);
@@ -941,7 +968,8 @@ static int set_quad_mode(struct spi_flash *flash, u8 idcode0)
                return micron_quad_enable(flash);
 #endif
        default:
-               printf("SF: Need set QEB func for %02x flash\n", idcode0);
+               printf("SF: Need set QEB func for %02x flash\n",
+                      JEDEC_MFR(info));
                return -1;
        }
 }
@@ -971,144 +999,29 @@ int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
 }
 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
 
-#ifdef CONFIG_SPI_FLASH_SPANSION
-static int spansion_s25fss_disable_4KB_erase(struct spi_slave *spi)
-{
-       u8 cmd[4];
-       u32 offset = 0x800004; /* CR3V register offset */
-       u8 cr3v;
-       int ret;
-
-       cmd[0] = CMD_SPANSION_RDAR;
-       cmd[1] = offset >> 16;
-       cmd[2] = offset >> 8;
-       cmd[3] = offset >> 0;
-
-       ret = spi_flash_cmd_read(spi, cmd, 4, &cr3v, 1);
-       if (ret)
-               return -EIO;
-       /* CR3V bit3: 4-KB Erase */
-       if (cr3v & 0x8)
-               return 0;
-
-       cmd[0] = CMD_SPANSION_WRAR;
-       cr3v |= 0x8;
-       ret = spi_flash_cmd_write(spi, cmd, 4, &cr3v, 1);
-       if (ret)
-               return -EIO;
-
-       cmd[0] = CMD_SPANSION_RDAR;
-       ret = spi_flash_cmd_read(spi, cmd, 4, &cr3v, 1);
-       if (ret)
-               return -EIO;
-       if (!(cr3v & 0x8))
-               return -EFAULT;
-
-       return 0;
-}
-#endif
-
 int spi_flash_scan(struct spi_flash *flash)
 {
        struct spi_slave *spi = flash->spi;
-       const struct spi_flash_params *params;
-       u16 jedec, ext_jedec;
-       u8 cmd, idcode[5];
-       int ret;
-       static u8 spi_read_cmds_array[] = {
-               CMD_READ_ARRAY_SLOW,
-               CMD_READ_ARRAY_FAST,
-               CMD_READ_DUAL_OUTPUT_FAST,
-               CMD_READ_QUAD_OUTPUT_FAST,
-               CMD_READ_DUAL_IO_FAST,
-               CMD_READ_QUAD_IO_FAST };
-
-       /* Read the ID codes */
-       ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
-       if (ret) {
-               printf("SF: Failed to get idcodes\n");
-               return ret;
-       }
-
-#ifdef DEBUG
-       printf("SF: Got idcodes\n");
-       print_buffer(0, idcode, 1, sizeof(idcode), 0);
-#endif
-
-       jedec = idcode[1] << 8 | idcode[2];
-       ext_jedec = idcode[3] << 8 | idcode[4];
-
-       /* Validate params from spi_flash_params table */
-       params = spi_flash_params_table;
-       for (; params->name != NULL; params++) {
-               if ((params->jedec >> 16) == idcode[0]) {
-                       if ((params->jedec & 0xFFFF) == jedec) {
-                               if (params->ext_jedec == 0)
-                                       break;
-                               else if (params->ext_jedec == ext_jedec)
-                                       break;
-                       }
-               }
-       }
-
-       if (!params->name) {
-               printf("SF: Unsupported flash IDs: ");
-               printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
-                      idcode[0], jedec, ext_jedec);
-               return -EPROTONOSUPPORT;
-       }
-
-#ifdef CONFIG_SPI_FLASH_SPANSION
-       /*
-        * The S25FS-S family physical sectors may be configured as a
-        * hybrid combination of eight 4-kB parameter sectors
-        * at the top or bottom of the address space with all
-        * but one of the remaining sectors being uniform size.
-        * The Parameter Sector Erase commands (20h or 21h) must
-        * be used to erase the 4-kB parameter sectors individually.
-        * The Sector (uniform sector) Erase commands (D8h or DCh)
-        * must be used to erase any of the remaining
-        * sectors, including the portion of highest or lowest address
-        * sector that is not overlaid by the parameter sectors.
-        * The uniform sector erase command has no effect on parameter sectors.
-        */
-       if (jedec == 0x0219 && (ext_jedec & 0xff00) == 0x4d00) {
-               int ret;
-               u8 id[6];
-
-               /* Read the ID codes again, 6 bytes */
-               ret = spi_flash_cmd(flash->spi, CMD_READ_ID, id, sizeof(id));
-               if (ret)
-                       return -EIO;
+       const struct spi_flash_info *info = NULL;
+       int ret = -1;
 
-               ret = memcmp(id, idcode, 5);
-               if (ret)
-                       return -EIO;
+       info = spi_flash_read_id(flash);
+       if (IS_ERR_OR_NULL(info))
+               return -ENOENT;
 
-               /* 0x81: S25FS-S family 0x80: S25FL-S family */
-               if (id[5] == 0x81) {
-                       ret = spansion_s25fss_disable_4KB_erase(spi);
-                       if (ret)
-                               return ret;
-               }
-       }
-#endif
        /* Flash powers up read-only, so clear BP# bits */
-       if (idcode[0] == SPI_FLASH_CFI_MFR_ATMEL ||
-           idcode[0] == SPI_FLASH_CFI_MFR_MACRONIX ||
-           idcode[0] == SPI_FLASH_CFI_MFR_SST)
+       if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_ATMEL ||
+           JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX ||
+           JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST)
                write_sr(flash, 0);
 
-       /* Assign spi data */
-       flash->name = params->name;
+       flash->name = info->name;
        flash->memory_map = spi->memory_map;
        flash->dual_flash = spi->option;
 
-       /* Assign spi flash flags */
-       if (params->flags & SST_WR)
+       if (info->flags & SST_WR)
                flash->flags |= SNOR_F_SST_WR;
 
-       /* Assign spi_flash ops */
 #ifndef CONFIG_DM_SPI_FLASH
        flash->write = spi_flash_cmd_write_ops;
 #if defined(CONFIG_SPI_FLASH_SST)
@@ -1123,52 +1036,46 @@ int spi_flash_scan(struct spi_flash *flash)
        flash->read = spi_flash_cmd_read_ops;
 #endif
 
-       /* lock hooks are flash specific - assign them based on idcode0 */
-       switch (idcode[0]) {
 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
-       case SPI_FLASH_CFI_MFR_STMICRO:
-       case SPI_FLASH_CFI_MFR_SST:
+       /* NOR protection support for STmicro/Micron chips and similar */
+       if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_STMICRO ||
+           JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) {
                flash->flash_lock = stm_lock;
                flash->flash_unlock = stm_unlock;
                flash->flash_is_locked = stm_is_locked;
-#endif
-               break;
-       default:
-               debug("SF: Lock ops not supported for %02x flash\n", idcode[0]);
        }
+#endif
 
        /* Compute the flash size */
        flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
+       flash->page_size = info->page_size;
        /*
         * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
         * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
         * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
         * have 256b pages.
         */
-       if (ext_jedec == 0x4d00) {
-               if ((jedec == 0x0215) || (jedec == 0x216))
-                       flash->page_size = 256;
-               else
+       if (JEDEC_EXT(info) == 0x4d00) {
+               if ((JEDEC_ID(info) != 0x0215) &&
+                   (JEDEC_ID(info) != 0x0216))
                        flash->page_size = 512;
-       } else {
-               flash->page_size = 256;
        }
        flash->page_size <<= flash->shift;
-       flash->sector_size = params->sector_size << flash->shift;
-       flash->size = flash->sector_size * params->nr_sectors << flash->shift;
+       flash->sector_size = info->sector_size << flash->shift;
+       flash->size = flash->sector_size * info->n_sectors << flash->shift;
 #ifdef CONFIG_SF_DUAL_FLASH
        if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
                flash->size <<= 1;
 #endif
 
+#ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
        /* Compute erase sector and command */
-       if (params->flags & SECT_4K) {
+       if (info->flags & SECT_4K) {
                flash->erase_cmd = CMD_ERASE_4K;
                flash->erase_size = 4096 << flash->shift;
-       } else if (params->flags & SECT_32K) {
-               flash->erase_cmd = CMD_ERASE_32K;
-               flash->erase_size = 32768 << flash->shift;
-       } else {
+       } else
+#endif
+       {
                flash->erase_cmd = CMD_ERASE_64K;
                flash->erase_size = flash->sector_size;
        }
@@ -1176,18 +1083,17 @@ int spi_flash_scan(struct spi_flash *flash)
        /* Now erase size becomes valid sector size */
        flash->sector_size = flash->erase_size;
 
-       /* Look for the fastest read cmd */
-       cmd = fls(params->e_rd_cmd & spi->mode_rx);
-       if (cmd) {
-               cmd = spi_read_cmds_array[cmd - 1];
-               flash->read_cmd = cmd;
-       } else {
-               /* Go for default supported read cmd */
-               flash->read_cmd = CMD_READ_ARRAY_FAST;
-       }
-
-       /* Not require to look for fastest only two write cmds yet */
-       if (params->flags & WR_QPP && spi->mode & SPI_TX_QUAD)
+       /* Look for read commands */
+       flash->read_cmd = CMD_READ_ARRAY_FAST;
+       if (spi->mode & SPI_RX_SLOW)
+               flash->read_cmd = CMD_READ_ARRAY_SLOW;
+       else if (spi->mode & SPI_RX_QUAD && info->flags & RD_QUAD)
+               flash->read_cmd = CMD_READ_QUAD_OUTPUT_FAST;
+       else if (spi->mode & SPI_RX_DUAL && info->flags & RD_DUAL)
+               flash->read_cmd = CMD_READ_DUAL_OUTPUT_FAST;
+
+       /* Look for write commands */
+       if (info->flags & WR_QPP && spi->mode & SPI_TX_QUAD)
                flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
        else
                /* Go for default supported write cmd */
@@ -1197,9 +1103,10 @@ int spi_flash_scan(struct spi_flash *flash)
        if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
            (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
            (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
-               ret = set_quad_mode(flash, idcode[0]);
+               ret = set_quad_mode(flash, info);
                if (ret) {
-                       debug("SF: Fail to set QEB for %02x\n", idcode[0]);
+                       debug("SF: Fail to set QEB for %02x\n",
+                             JEDEC_MFR(info));
                        return -EINVAL;
                }
        }
@@ -1224,13 +1131,13 @@ int spi_flash_scan(struct spi_flash *flash)
        }
 
 #ifdef CONFIG_SPI_FLASH_STMICRO
-       if (params->flags & E_FSR)
+       if (info->flags & E_FSR)
                flash->flags |= SNOR_F_USE_FSR;
 #endif
 
        /* Configure the BAR - discover bank cmds and read current bank */
 #ifdef CONFIG_SPI_FLASH_BAR
-       ret = spi_flash_read_bar(flash, idcode[0]);
+       ret = spi_flash_read_bar(flash, info);
        if (ret < 0)
                return ret;
 #endif