static int alt_epcs_flash_query(alt_flash_epcs_dev* flash)
{
  int ret_code = 0;

  /* Decide if an epcs flash device is attached.
   *  
   * ret_code = -ENODEV = No device found!
   */

  /* It may be necessary to unlock or lock sectors,
   * reset the device, or whatever, to ensure that
   * it's in a known working state.
  */

  /* Send the RES command sequence */
  flash->silicon_id =
    epcs_read_electronic_signature(flash->register_base);

  /* Fill in all device-specific parameters. */
  if (flash->silicon_id == 0x16) /* EPCS64 */
  {
    flash->dev.region_info[0].region_size = 64 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 128;
    flash->dev.region_info[0].block_size = 65536;
  }
  else if (flash->silicon_id == 0x14) /* EPCS16 */
  {
    flash->dev.region_info[0].region_size = 16 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 32;
    flash->dev.region_info[0].block_size = 65536;
  }
  else if (flash->silicon_id == 0x13) /* EPCS8 */
  {
    flash->dev.region_info[0].region_size = 8 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 16;
    flash->dev.region_info[0].block_size = 65536;
  }
  else if (flash->silicon_id == 0x12) /* EPCS4 */
  {
    flash->dev.region_info[0].region_size = 4 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 8;
    flash->dev.region_info[0].block_size = 65536;
  }
  else if (flash->silicon_id == 0x10) /* EPCS1 */
  {
    flash->dev.region_info[0].region_size = 1 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 4;
    flash->dev.region_info[0].block_size = 32768;
  }
  else
  {
    /* 
     * Read electronic signature doesn't work for the EPCS128; try 
     * the "Read Device ID" command" before giving up.
     */
    flash->silicon_id = epcs_read_device_id(flash->register_base);
    
    if(flash->silicon_id == 0x18) /* EPCS128 */
    {
      flash->dev.region_info[0].region_size = 128 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 64;
      flash->dev.region_info[0].block_size = 262144;     
    }
    else 
    {
      ret_code = -ENODEV; /* No known device found! */
    }
  }

  flash->size_in_bytes = flash->dev.region_info[0].region_size;
  flash->dev.number_of_regions = 1;
  flash->dev.region_info[0].offset = 0;
  flash->page_size = 256;

  /* Consider clearing all BP bits here. */
  return ret_code;
}
static int alt_epcs_flash_query(alt_flash_epcs_dev* flash)
{
  int ret_code = 0;

  /* Decide if an epcs flash device is attached.
   *  
   * ret_code = -ENODEV = No device found!
   */

  /* It may be necessary to unlock or lock sectors,
   * reset the device, or whatever, to ensure that
   * it's in a known working state.
  */
  
  /* Disable 4-bytes address mode. */
  flash->four_bytes_mode = 0;
  
  /* Send the RES command sequence */
  flash->silicon_id =
    epcs_read_electronic_signature(flash->register_base);

  /* Fill in all device-specific parameters. */
  if (flash->silicon_id == 0x16) /* EPCS64 */
  {
    flash->dev.region_info[0].region_size = 64 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 128;
    flash->dev.region_info[0].block_size = 65536;
  }
  else if (flash->silicon_id == 0x14) /* EPCS16 */
  {
    flash->dev.region_info[0].region_size = 16 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 32;
    flash->dev.region_info[0].block_size = 65536;
  }
  else if (flash->silicon_id == 0x13) /* EPCS8 */
  {
    flash->dev.region_info[0].region_size = 8 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 16;
    flash->dev.region_info[0].block_size = 65536;
  }
  else if (flash->silicon_id == 0x12) /* EPCS4 */
  {
    flash->dev.region_info[0].region_size = 4 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 8;
    flash->dev.region_info[0].block_size = 65536;
  }
  else if (flash->silicon_id == 0x10) /* EPCS1 */
  {
    flash->dev.region_info[0].region_size = 1 * 1024 * 1024 / 8;
    flash->dev.region_info[0].number_of_blocks = 4;
    flash->dev.region_info[0].block_size = 32768;
  }
  else
  {
    /* 
     * Read electronic signature doesn't work for newer devices; try 
     * the "Read Device ID" command" before giving up.
     */
    flash->silicon_id = epcs_read_device_id(flash->register_base);
    
    /*
     * Last byte is the density ID. Note the difference between
     * EPCS128 and EPCQ128 -- arranged differently, though the 
     * least significant byte of each is '0x18'.
     */
    if((flash->silicon_id & 0xFFFFFF) == 0x20BA15) /* EPCQ16 */
    {
      flash->dev.region_info[0].region_size = 16 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 32; /* number of sectors */
      flash->dev.region_info[0].block_size = 65536;  /* sector size */
    }
    else if((flash->silicon_id & 0xFFFFFF) == 0x20BA16) /* EPCQ32 */
    {
      flash->dev.region_info[0].region_size = 32 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 64; /* number of sectors */
      flash->dev.region_info[0].block_size = 65536;  /* sector size */
    }
    else if((flash->silicon_id & 0xFFFFFF) == 0x20BA17) /* EPCQ64 */
    {
      flash->dev.region_info[0].region_size = 64 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 128; /* number of sectors */
      flash->dev.region_info[0].block_size = 65536;  /* sector size */
    }
    else if((flash->silicon_id & 0xFFFFFF) == 0x20BA18) /* EPCQ128 */
    {
      flash->dev.region_info[0].region_size = 128 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 256; /* number of sectors */
      flash->dev.region_info[0].block_size = 65536;  /* sector size */
    }
    else if((flash->silicon_id & 0xFF) == 0x18) /* EPCS128 */
    {
      flash->dev.region_info[0].region_size = 128 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 64;
      flash->dev.region_info[0].block_size = 262144;
    }
    else if((flash->silicon_id & 0xFF ) == 0x19) /* EPCQ256 */
    {
      flash->dev.region_info[0].region_size = 256 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 512; /* number of sectors */
      flash->dev.region_info[0].block_size = 65536;  /* sector size */

      /* Enable 4-bytes address mode if the device density is greater than 256Mbit. 
       * Last byte of device ID is density ID.
       *
       * The whole 4-bytes thing extends commands that send a memory address to
       * the chip (read, write, erase) from 3 bytes to 4. The 4-byte address mode
       * must first be programmed into the device, though. To complicate things, 
       * other Altera IP expects the chip to be in 3 byte address mode when they 
       * start using it. To be nice, we'll place the device into 4-byte address mode
       * when we need to, and take it back out when we're done.
       */
      flash->four_bytes_mode = 1;
    }
    else if((flash->silicon_id & 0xFF ) == 0x20) /* EPCQ512 */
    {
      flash->dev.region_info[0].region_size = 512 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 1024; /* number of sectors */
      flash->dev.region_info[0].block_size = 65536;  /* sector size */

      /* Enable 4-bytes address mode if the device density is greater than 256Mbit. */
      flash->four_bytes_mode = 1;
    }
    else if((flash->silicon_id & 0xFF ) == 0x21) /* EPCQ1024 */
    {
      flash->dev.region_info[0].region_size = 1024 * 1024 * 1024 / 8;
      flash->dev.region_info[0].number_of_blocks = 2048; /* number of sectors */
      flash->dev.region_info[0].block_size = 65536;  /* sector size */

      /* Enable 4-bytes address mode if the device density is greater than 256Mbit. */
      flash->four_bytes_mode = 1;
    }
    else 
    {
      ret_code = -ENODEV; /* No known device found! */
    }
  }
  
  flash->size_in_bytes = flash->dev.region_info[0].region_size;
  flash->dev.number_of_regions = 1;
  flash->dev.region_info[0].offset = 0;
  flash->page_size = 256;

  /* Consider clearing all BP bits here. */
  return ret_code;
}